The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)

 

   

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


   
 

Категории