http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls#Understanding_blocks.2C_Procs_and_methods
In short, the difference between lambda and Proc is in whether the number of parameters is checked and the behavior of "return"
The resulting Proc object created by lambda checks the number of arguments and throws an ArgumentError. A Proc object created by Proc.new does not check the number of arguments.
The example from the link is:
lamb = lambda {|x, y| puts x + y}
pnew = Proc.new {|x, y| puts x + y}
# works fine, printing 6
pnew.call(2, 4, 11)
# throws an ArgumentError
lamb.call(2, 4, 11)
Second, a return from Proc.new returns from it's enclosing method,
while a return from a lambda Proc acts more "conventionally", returning to it's caller.
Here's an interesting quote "Blocks, as I see them, are unborn Procs. Blocks are the larval, Procs are the insects."
And an explanation of closure, a closely related concept:
http://en.wikipedia.org/wiki/Closure_(computer_science)
It has a ruby example of the difference of "return" behavior between lambda and Proc.
No comments:
Post a Comment