For example instead of:
["This", "blog", "is", "great"].map {|w| w.upcase}
=> ["THIS", "BLOG", "IS", "GREAT"]
We could simplify it by:
["This", "blog", "is", "great"].map(&:upcase)
=> ["THIS", "BLOG", "IS", "GREAT"]
What ruby ends up do is calling the to_proc method on that symbol and it coerces into a block!
Here is a link to the documentation and for those who are lazy I have posted the source below.
# File lib/extensions/symbol.rb, line 23
def to_proc
proc { |obj, *args| obj.send(self, *args) }
end
No comments:
Post a Comment