order/format of params in method definition


/ Published in: Ruby
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.