This article mainly introduces the two differences between Proc and Lambda in Ruby, this article explains that in proc and lambda, the return keyword has different meanings, check the parameters of the different ways two important differences, need friends can refer to the
1. In proc and lambda, the return keyword has different meanings:
In proc, return only means returning from this lambda.
In a lambda, return is not returned from the proc, but rather from the scope in which the proc is defined.
The code is as follows:
def One_method
p = Proc.new{return # When calling this block, returns 10 directly from the scope of P, so the following return will not execute
Result = P.call
Return result * 2
End
Def Two_method
p = Lambda{return # when calling this block, returning 10 from the lambda
Result = P.call
Return result * 2 #Continue execution
End
Puts One_method # 10
Puts Two_method # 20
2, in proc and lambda, check the parameters in different ways:
In proc, if the parameter is more than defined, the redundant parameter is ignored, and if the parameter is less than defined, the parameter not passed over is automatically specified as nil.
In a lambda, a argumenterror error is thrown regardless of whether the actual argument is more or less than the defined parameter.