Read more

Short lambda syntax in Ruby 1.9

Henning Koch
July 17, 2013Software engineer at makandra GmbH

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

twice = -> (x) { 2 * x }
twice.call(5) # => 10
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

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 }.

Posted by Henning Koch to makandra dev (2013-07-17 08:49)