method

lambda

v1_8_6_287 - Show latest stable - Class: Kernel
lambda()
public

Equivalent to Proc.new, except the resulting Proc objects check the number of parameters passed when called.

2Notes

Difference in the way returns are handled

x2es · Oct 6, 20111 thank

Also, there is a difference in the way returns are handled from the Proc. A return from Proc.new returns from the enclosing method. Return in lambda-block acts like in regular method.

=== return example def proc_return Proc.new { return "Proc.new"}.call return "proc_return method finished" end

def lambda_return
lambda { return "lambda" }.call
return "lambda_return method finished"
end
puts proc_return
puts lambda_return
# => Proc.new
# => lambda_return method finished

Usage example

szeryf · Jul 16, 2009

Usage example: cube = lambda {|x| x * x * x } cube.call(3) # => 27 cube.call(6) # => 216