my_proc = Proc.new { |x| "Value: #{x}" } my_lambda = ->(x) { "Value: #{x}" } puts my_proc.call puts my_lambda.call
Procs are lenient with arguments and assign nil if missing, so my_proc.call outputs "Value: ". Lambdas enforce arity strictly, so my_lambda.call raises an ArgumentError.
def test_proc p = Proc.new { return "From Proc" } p.call return "From method" end def test_lambda l = -> { return "From Lambda" } l.call return "From method" end puts test_proc puts test_lambda
A return inside a Proc exits the enclosing method immediately, so test_proc returns "From Proc". A return inside a lambda only exits the lambda, so test_lambda continues and returns "From method".
my_proc = Proc.new { |a, b| a + (b || 0) } my_lambda = ->(a, b) { a + b } puts my_proc.call(5) puts my_lambda.call(5)
Lambdas check the number of arguments strictly and raise an error if the count doesn't match. Procs are lenient and assign nil to missing arguments, so my_proc.call(5) works but my_lambda.call(5) raises ArgumentError.
def outer p = Proc.new { return "Proc return" } l = -> { return "Lambda return" } result_p = p.call result_l = l.call "Outer method end" end puts outer
The return inside the Proc exits the entire outer method immediately, so outer returns "Proc return". The lambda's return is never reached.
Lambdas enforce arity strictly and a return inside a lambda exits only the lambda itself. Procs are lenient with arguments and a return inside a proc exits the enclosing method.