Posted By

wdso on 04/23/09


Tagged

Enumeration


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

wdso
webstic


Iterator shortcut using symbols


 / Published in: Ruby
 

Extracted from "The Ruby Way: Solutions and Techniques in Ruby Programming."

  1. class Symbol
  2. def to_proc
  3. proc {|obj, *args| obj.send(self, *args) }
  4. end
  5. end
  6.  
  7. # map takes a block, & causes the interpreter to invoke to_proc on given symbol
  8. # symbol is passed as message to the object beeing processed
  9. # in other words, this is a short way of writing:
  10. # list = words.map {|x| x.capitalize }
  11. list = words.map(&:capitalize)

Report this snippet  

You need to login to post a comment.