liveBook · Manning

Lambda

As explained in The Well-Grounded Rubyist, Fourth Edition

A lambda is a Ruby callable object representing an anonymous function with lambda-specific argument and return behavior.

Need a callable multiplication operation to store and use later? `mult = ->(x, y) { x * y }` creates a lambda; `mult.call(3, 4)` returns `12`. A lambda is an anonymous function represented by a `Proc` object, not an instance of a separate `Lambda` class. You can also write `lambda { |x, y| x * y }`; with `->`, parameters go in parentheses before the body. section 14.2

>> lam = lambda {|x| p x }
=> #<Proc:0x000001029901f8@(irb):3 (lambda)>
>> lam.call(1)
1
=> 1
>> lam.call
wrong number of arguments (given 0, expected 1) (ArgumentError)
>> lam.call(1,2,3)
wrong number of arguments (given 3, expected 1) (ArgumentError)

Try the one-argument lambda with one, zero, and three arguments. Only the matching call succeeds. section 14.2

Lambdas and ordinary procs are both callable, but three differences matter:

  • A lambda checks its argument count. An ordinary one-parameter proc accepts missing arguments as `nil` and ignores extras. section 14.1.6 section 14.2
  • Create a lambda explicitly with `lambda` or `->`. Capturing a method's block with `&block` instead creates an ordinary proc. section 14.2
  • `return` inside a lambda exits the lambda, so execution after its call can continue. In an ordinary proc, `return` exits the surrounding method; outside a method, it can raise an error. section 14.2

Make reusable filters

Instead of writing separate methods for multiples of three and five, define `find_multiples = ->(x, arr) { arr.select { it % x == 0 } }`. Then `find_multiples_of = find_multiples.curry` and `find_multiples_of_3 = find_multiples_of.(3)` produce a callable waiting for an array. Calling it with `[-3, 3, 4, 5, 6, 8, 9, 10, 12]` returns `[-3, 3, 6, 9, 12]`. `curry` lets this one lambda supply several related operations. section 16.3.5

Because a lambda is a proc object, it can be stored and called later. A proc can also serve as a method's block using `&`; that differs from capturing a supplied block, which does not implicitly create a lambda. section 14.1 section 14.1.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains construction, strict arity, explicit creation, and return behavior through calls and contrasts, then applies currying without detailing Proc internals.
Examples
Tests missing and extra arguments, contrasts return behavior, multiplies two values, and curries an array filter.
What it assumes
Assumes familiarity with Ruby methods, blocks, and arguments while establishing lambda creation and its contrast with ordinary procs.
What you can do after
Build it from scratch with `lambda` or `->`, and apply `curry` to make reusable functions.
How it connects
Connects lambdas to Proc objects, code blocks, arity, return, currying, and partial function application.

Also explained in

Compare how 18 books explain lambda →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.