David A. Black wrote:
> Hi --
> 
> I'd always thought that yield was just syntactic sugar for calling the
> block, but I notice there's at least one difference:
> 
>   def x(&b)
>     b.call
>     puts "Still here (x)"
>   end

b is a Proc, not a block, so it can be stored somewhere, and called
after returning from x and from the context in which the block is
defined, so return from b should not try to return from that context. I
guess that's why the return only returns from the Proc.

>   def y
>     yield
>     puts "Still here (y)"
>   end

In this case, the yield transfers control to a particular dynamic
context, in which "return" can safely be interpreted as return from the
top level (at which lam is a local variable). Hence the error (the same
as if you called return directly from the top level).

>   lam = lambda { return }
> 
>   x &lam
>   y &lam
> 
>   Output:
> 
>   Still here (x)
>   retu.rb:11: unexpected return (LocalJumpError)
>           from retu.rb:11:in `y'
>           from retu.rb:14
> 
> 
> I'm trying to sort out all the return/Proc/lambda combinations... and
> this one struck me as odd.
> 
> 
> David
> 


-- 
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407