ruby - Differences between Proc and Lambda -
ruby has differences between procs created via proc.new
, lambda
(or ->()
operator in 1.9). appears non-lambda procs splat array passed in across block arguments; procs created via lambda not.
p = proc.new { |a,b| + b} p[[1,2]] # => 3 l = lambda { |a,b| + b } l[[1,2]] # => argumenterror: wrong number of arguments (1 2)
does have insight motivations behind behavior?
there 2 main differences between lambdas , non-lambda proc
s:
- just methods, lambdas return themselves, whereas non-lambda
proc
s return enclosing method, blocks. - just methods, lambdas have strict argument checking, whereas non-lambda
proc
s have loose argument checking, blocks.
or, in short: lambdas behave methods, non-lambda proc
s behave blocks.
what seeing there instance of #2. try block , method in addition non-lambda proc
, lambda, , you'll see. (without behavior, hash#each
real pita use, since does yield array two-elements, pretty want treat 2 arguments.)
Comments
Post a Comment