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

















