passing objects to methods as blocks in ruby
Lambda’s can come in handy when you need to apply a similar mutation to a collection of objects.
Here’s an example of a simple operation but the block can be as complicated or generic as you need.
ints_to_floats = lambda { |i| i.to_f }
(0..5).map(&ints_to_floats)
#=> [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
In ruby 1.9.2 and monkey patched in by rails, Symbol#to_proc is defined as:
Proc.new { |*args| args.shift.__send__(self, *args) }
So our earlier exmaple can be replaced with
(0..5).map(&:to_f)
#=> [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
So thats the trick behind that ruby magic.