On Monday, October 3, 2011 at 3:01 AM, "Martin J. Drst" wrote:
> On 2011/10/02 9:51, Urabe Shyouhei wrote:
>  
> > (10/01/2011 04:53 PM), Chuck Remes wrote:
>  
> > > - Remove confusing proc/lambda/Proc differences
> >  
> > proc is Proc and lambda is ->(){}. No confusion now.
>  
> That's easy for people who know what a proc is, and what a lambda is.  
> But that may not be everybody.
>  
> A simplification in this area was on my wishlist, too. At the time, as  
> far as I remember it, there were at least two axes on which these and  
> simila constructs differed.
>  
> One axis was the handling of parameters/attributes: strictly positional 
> or with all kinds of bells and whistles (splats, defaults,...), and  
> whether a mismatch between actual and formal attributes triggered an  
> error or not.
>  
> Another axis was what happened in the case of a return: Jump out of the 
> construct only or also jump out of the surrounding method. There were  
> also related differences for break and next.
>  
> As far as I understand, there was quite a bit of work on the first axis.  
> But if I look at the 1.8 and 1.9 Pickaxe books, the 1.8 book spends a  
> bit less than 3 pages on Blocks, Closures, and Proc Objects, whereas the  
> 1.9 book spends 4 pages on the same topic.
>  
> Anyway, if we get to a point where we can say:
>  
> proc is Proc and does ..., and lambda is ->(){} and does ....
> (where the '...' parts are extremely simple), then and only then can we 
> say "No confusion now.".

Here is a helpful way to think about it:

A lambda is an anonymous function. Like functions they have strict arity checking (you must pass the number of arguments specified) and they create their own return scope (i.e. return takes you out of the lambda/function).

A proc is a block of code. As with any vanilla do¡Äend construction in ruby, the number of arguments on the block does not need to match with what is passed to the block. Also, like do¡Äend, procs do not create their own return scope. You shouldn't return from a proc if you wouldn't return from a do¡Äend in the same position (i.e. never).

Is that clearer?

- Josh