Short lambda syntax in Ruby 1.9

Posted . Visible to the public.

Ruby 1.9 brings a shorter way to define lambdas using the -> operator:

twice = -> (x) { 2 * x }
twice.call(5) # => 10

This is equivalent to:

twice = lambda {|x| 2 * x }
twice.call(5) # => 10

Note that the syntax is subtly different from Coffeescript where you define function parameters before the arrow: (x) -> { 2 * x }.

Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2013-07-17 06:49)