Posted By

cczona on 03/24/08


Tagged

method ruby parameter


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


order/format of params in method definition


 / Published in: Ruby
 

See also p. 348 ("Invoking a Method") of Pickaxe 2nd edition

  1. def name(params, hash, *array, &proc );
  2. puts params.inspect, hash.inspect, array.inspect, proc.inspect
  3. end
  4.  
  5. name(1, 2=>3, 4=>5, *[6,7,8]) {9} #disambiguate array
  6. puts
  7. name(1, {2=>3, 4=>5}, 6,7,8) {9} #disambiguate hash
  8. puts
  9. name(1, {2=>3, 4=>5}, 6,7,8, &lambda {9}) #called with lambda instead of block
  10. puts
  11. name(1, 2=>3, 4=>5, *[6,7,8], &Proc.new {9}) #or proc instead of block
  12.  
  13. # 1
  14. # {2=>3, 4=>5}
  15. # [6, 7, 8]
  16. # #<Proc:...>

Report this snippet  

You need to login to post a comment.