Hack of the day: A reverse for Rails' &:

Updated . Posted . Visible to the public.

Ever wondered if there is a reverse for Rails' .each(&:to_s) idiom? Turns out there is...

You probably already know, that you can abbreviate code like

dogs.each { |dog| dog.bark! }

with

dogs.each(&:bark!)

Now suppose it is the other way round and you have

bones.each { |bone| dog.eat!(bone) }

Good old Ruby already has a solution:

bones.each(&dog.method(:eat!))
Tobias Kraze
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2011-06-30 14:19)