Hi --

On Mon, 8 Aug 2005, Joel VanderWerf wrote:

> Nikolai Weibull wrote:
> ...
>> module Kernel
>>   def returning(value)
>>     yield value
>>     value
>>   end
>> end
> ...
>
> Why call it #returning and limit its usefulness? The method can be used
> just as well in assignments:
>
> ary = returning [] do ... end
>
> or as an argument to a method or proc.

It's still "returning []" in the sense that it's going to return its
argument, even if the block's last expression does not evaluate to
that argument.  It's #returning whose return value "returning obj"
refers to.  You can, but do not have to, use it as the return value of
a method in which it appears.

For example:

   def x
     ary = returning [] do |a|
       a.concat %w{a b c d e}
       puts "Done with this block!"
     end
     p ary
     return 100    # or whatever
   end

   x
      =>  Done with this block!
          ["a", "b", "c", "d", "e"]

Sidenote: it's also interesting how close this gets to:

   [].instance_eval {|ary| ... }

(though obviously not identical).  I can't think of a good name for:

   [].yield_me_and_return_me {|ary| ... }

though :-)


David

-- 
David A. Black
dblack / wobblini.net