The two differences between proc and lambda IN Ruby: proclamloud
1. In proc and lambda, the return keyword has different meanings.:
In proc, return only indicates returning from this lambda.
In lambda, return is not returned from proc, but from the scope that defines proc.
Copy codeThe Code is as follows:
Def one_method
P = Proc. new {return 10} # When this block is called, 10 is directly returned from the scope of p, so the following return will not be executed
Result = p. call
Return result * 2
End
Def two_method
P = lambda {return 10} # return 10 from lambda when calling this block
Result = p. call
Return result * 2 # continue execution
End
Puts one_method #10
Puts two_method #20
2. In proc and lambda, check parameters in different ways:
In proc, if there are more parameters than defined, extra parameters are ignored. If there are fewer parameters than defined, the parameter that has not been passed is automatically specified as nil.
In lambda, an ArgumentError error is thrown no matter whether the actual parameter is more or less than the defined parameter.