Parameters and Default Arguments

 
   

Ruby Way
By Hal Fulton
Slots : 1.0
Table of Contents
 


Although varying-sized parameters lists are available with *, Ruby does not have Python's ** for keyword arguments. Currently, keyword arguments are only available in a limited sense by passing a hash in as a single parameter. In such a case, the enclosing braces may be omitted.

Ruby allows for a special block parameter, indicated by the & prefix.

 

 def doit(*args, &code)  code.call(*args) end doit(1,2,3){ |*x| puts "You sent me #{ x.size}  values." } # Output: You sent me 3 values. 

Ruby does not cache method definitions as Python does. Even default arguments are evaluated for each call.

 

 class Foo  def val(); 2; end  def show(v=val())  puts v  end end a=Foo.new() a.show()    # 2 class Foo  def val()  3  end end a.show()    # 3 


   

 

 



The Ruby Way
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2000
Pages: 119
Authors: Hal Fulton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net