D
David A. Black
Hi --
Yes, assuming that by "proc" you mean Proc.new and not proc....
The problem is that proc also rhymes with Proc
David
--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
Another difference between lambdas and procs, is that procs are less
strict about arity when they are called.
Yes, assuming that by "proc" you mean Proc.new and not proc....
1: p = Proc .new {|x,y| [x,y]}
2: l = lambda {|x,y| [x, y]}
3:
4: p.call(1) # => [1, nil]
5: l.call # =>
# ~> -:5: wrong number of arguments (0 for 2) (ArgumentError)
# ~> from -:5:in `call'
# ~> from -:5
procs are invoked using the same semantics as a yield, which acts like
parallel assignment with nils being provided for missing arguments.
lambdas are invoked using the same semantics as a message send.
Since it's a bit confusing to have proc and Proc.new be different,
this has been changed in 1.9: proc and Proc.new are the same in 1.9,
and lambda is different.
(I still have no idea how to memorize which is which.)
Thinking about this a bit, I think that the basic rule is that procs
act like blocks, in that:
arguments are passed to them using yield semantics
and
return effects a return from the activation of the code containing
the source of the block
break breaks out of the block into the activation of the code
containing the source of the block
Lambdas act like methods in that:
arguments are passed to them using method call semantics
and
return effects a return from the body of the lambda
break breaks out of the body of the lambda, back to the caller,
effectively becoming a return
So a double mnemonic might be:
proc rhymes with block
lambda and method both have and m while proc does not.
The problem is that proc also rhymes with Proc
David
--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!