120 likes | 294 Views
Method and Parentheses. Optional Parentheses. Parentheses are omitted from method invocations in many common Ruby idioms. puts “Hello World” puts(“Hello World”) greeting = “Hello” size = greeting.length #equivalent: #size=greeting.length(). Parentheses are very commonly omitted when
E N D
Optional Parentheses Parentheses are omitted from method invocations in many common Ruby idioms. puts “Hello World” puts(“Hello World”) greeting = “Hello” size = greeting.length #equivalent: #size=greeting.length()
Parentheses are very commonly omitted when there are zero or one arguments to the invoked method. Although it is less common, the parentheses may be omitted even when there are multiple arguments, as in the following code: x = 3 # x is a number x.between? 1,5 # same as x.between?(1,5) def sum x, y #method definitions x+y end
Required Parentheses puts(sum 2,2) # Does this mean puts(sum(2,2)) or # puts(sum(2), 2)? puts factorial x # This can only # mean puts(factorial(x))
When you do use parentheses in a method invocation, the opening parenthesis must immediately follow the method name, with no intervening space. square(2+2)*2 # square(4)*2 = 16*2 = 32 square (2+2)*2 # square(4*2) = square(8) = 64
Default Value Argument A default parameter value can be specified during method definition to replace the value of a parameter if it is not passed into the method or the parameter's value is nil. def prefix(s, len=1) s[0,len] end prefix("Ruby", 3) # =>"Rub“ prefix("Ruby") # => "R"
Variable Length Argument List The last parameter of a method may be preceded by an asterisk(*), which is sometimes called the 'splat‘ operator. This indicates that more parameters may be passed to the function. Those parameters are collected up and an array is created. def calculate_value(x,y,*otherValues) puts otherValues end calculate_value(1,2,'a','b','c') ['a', 'b', 'c'].
Array Argument The asterisk operator may also precede an Array argument in a method call. In this case the Array will be expanded and the values passed in as if they were separated by commas. arr = ['a','b','c'] calculate_value(*arr) has the same result as: calculate_value('a','b','c')
Hash Argument Another technique that Ruby allows is to give a Hash when invoking a function, and that gives you best of all worlds: named parameters, and variable argument length. def accepts_hash( var ) print "got: ", var.inspect # will print out what it received End accepts_hash :arg1 => 'giving arg1', :argN =>'giving argN' # => got: {:argN=>"giving argN“, #:arg1=>"giving arg1"}
Parentheses for the Arguments,Hash Argument • Parentheses can be omitted for the arguments • If the last argument is a Hash, braces { } can be omitted. The following two work the same. #No parens for the argument list, no { } for a #hash accepts_hash :arg1=>'giving arg1', :argN=>'giving argN‘ # argument list enclosed in parens accepts_hash(:arg1=>'giving arg1', :argN='giving argN') # hash is explicitly created accepts_hash({:arg1=>'giving arg1', :argN=>'giving argN'})
Code Block If you are going to pass a code block to function, you need parentheses. accepts_hash( :arg1 => 'giving arg1', :argN => 'giving argN' ) { |s| puts s } accepts_hash( { :arg1 => 'giving arg1', :argN => 'giving argN' } ) {|s| puts s }