Lambda
In this page:
Lambda
A lambda (created with lambda { ... } or the -> "stabby lambda" syntax) is similar to a Proc but behaves more like a regular method: it enforces the exact number of arguments, and return inside it only exits the lambda itself, not the enclosing method. Lambdas are generally preferred over Procs when you want method-like strictness and behavior. Both Proc and Lambda are instances of the same Proc class under the hood.
Note: The ->(x) { x * x } stabby lambda syntax is the more modern, concise way to write a lambda compared to lambda { |x| x * x }.
Example: Lambda
square = lambda { |x| x * x }
cube = ->(x) { x * x * x }
puts square.call(4)
puts cube.call(3)
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: