Passing Arguments

Occasionally, you’ll create methods you call without passing any information, but usually you want methods to do something specific to your needs. When you call a method, you can supply one or several arguments that give the method information to process. When you use arguments in a method’s definition, you have to enumerate them inside parentheses, like the add method in the following example:

 def​ ​add​(x, y)
 # return x + y # return is optional
  x + y
 end
 
add(2, 3) ​# => 5
add 2, 3 ​# => 5
add(1.0, 3.14) ​# => 4.14
add(​"Hello "​, ​"Crystal"​) ​# => "Hello Crystal"
add(42, ​" times"​) ​# => Error: no overload matches 'Int32#+' with type String
add ​# => Error: wrong number of arguments for 'add'
 # (given 0, expected 2)

(You might recognize this from More Safety Through Types.)

When you’re calling a method, you can omit the ( ) if you prefer, as in line ​​. This is nice when you have only a few arguments, but it quickly becomes less readable in more complex situations. This book uses both ways as appropriate.

From lines ​​, ​​, and ​​, you see that we can happily use duck typing, as in any dynamic language. In duck typing, you let the language figure out the type of an object based on its content. You can use the add method for all objects x and y whose Type has a + method, which is the case here for numbers and strings.

As you’ll see shortly, you can specify types, but Crystal vows never to make typing arguments mandatory, unlike most statically typed languages. Duck typing is flexible and makes your code more widely applicable. But sometimes the given parameters aren’t suitable, as in line ​​. Then, Crystal relentlessly cuts execution short through a compiler error. Also, as in line ​​, when you give no or not enough arguments, another compiler error is issued: wrong number of arguments for ’add’ (given 0, expected 2).

In More Safety Through Types in the overloading.cr scripts, which also used the add method, we found out that Crystal allows typing of the arguments. (It might be a good idea to play with it again before continuing.)

Pitfall

images/aside-icons/warning.png

In the following snippet, only the y argument is declared to be of type Int, not x:

 def​ ​add​(x, y : Int)
  x + y
 end
 
 add 3, 4 ​# 7
 add 2.14, 7 ​# 9.14

In the second add call, x isn’t of type Int. If x and y should both be of type Int, define the method as: add(x : Int, y : Int).

Crystal offers some more intricate options for working with arguments. You can specify default values for arguments at the end of the parameter list, in case a parameter for that argument isn’t given, as in this show method:

 def​ ​show​(x, y = 1, z = 2, w = 3)
 "x: ​​#{​x​}​​, y: ​​#{​y​}​​, z: ​​#{​z​}​​, w: ​​#{​w​}​​"
 end
 
 show 10 ​# => "x: 10, y: 1, z: 2, w: 3"
 show 10, 10 ​# => "x: 10, y: 10, z: 2, w: 3"
 show 10, 30, 2, 3 ​# => "x: 10, y: 30, z: 2, w: 3"
 show 10, 20 ​# => "x: 10, y: 20, z: 2, w: 3"
 
 show 10, ​z: ​10 ​# => "x: 10, y: 1, z: 10, w: 3"
 show 10, ​w: ​30, ​y: ​2, ​z: ​3 ​# => "x: 10, y: 2, z: 3, w: 30"
 show ​y: ​10, ​x: ​20 ​# => "x: 20, y: 10, z: 2, w: 3"
 show ​y: ​10 ​# Error, missing argument: x

As you can see in the second series of show calls in the code above, you can specify arguments by name when the method is called. Using named arguments implies we aren’t tied to their order. Code is also much more readable if we use descriptive names, as in the following example where we construct an authorization client that needs values for the host, client_id, and client_secret arguments:

 require ​"oauth2"
 
 client = OAuth2::Client.​new​(
 host: ​​"martian1"​,
 client_id: ​​"7594"​,
 client_secret: ​​"W*GDFUY75HSVS#@!"
 )
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.61.170