1 / 12

Method and Parentheses

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

tariq
Download Presentation

Method and Parentheses

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Method and Parentheses

  2. 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()

  3. 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

  4. 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))

  5. 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

  6. Method and Arguments

  7. 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"

  8. 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'].

  9. 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')

  10. 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"}

  11. 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'})

  12. 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 }

More Related