Posted about 6 years ago. Visible to the public. Repeats.

Defining and calling lambdas or procs (Ruby)

There are different ways to define a lambda or proc in ruby:

  1. with lambda-keyword

    Copy
    test = lambda do |arg| puts arg end
  2. with the lambda literal -> (since ruby 1.9.1)

    Copy
    test = -> (arg) do puts arg end
  3. with the proc keyword (which defines a lambda that does not test the given number of arguments):

    Copy
    test = lambda do |arg| puts arg end

And there are different ways to call them:

  1. call (we prefer this)

    Copy
    test.call('hello world')
  2. Square brackets (could easily be mistaken for a hash)

    Copy
    test['hello world']
  3. .() (weird, isn't it?)

    Copy
    test.('hello world')
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Judith Roth
Last edit:
almost 2 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Judith Roth to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more