Iterator shortcut using symbols


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

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


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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.