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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)