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 procs:

  1. just methods, lambdas return themselves, whereas non-lambda procs return enclosing method, blocks.
  2. just methods, lambdas have strict argument checking, whereas non-lambda procs have loose argument checking, blocks.

or, in short: lambdas behave methods, non-lambda procs 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

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -