Inline Enumerator Messages in Ruby
Many rubyists are familiar with the following shorthand:
# normal
collection.each {|obj| obj.delete}
# shorthand
collection.each(&:delete)
This shorthand makes it easy to send each element of an enumerator a specific message.
But what if you want to send each element of your collection to a method on another object?
For example, what if you had an array of bill amounts and you wanted to map this to an array of Bill objects? You could:
amounts.map do |amount|
Bill.new(amount)
end
The short way to do this, however, is this:
amounts.map(&Bill.method(:new))
Just a cool trick!