"Saynatkari" <ruby-ml / magical-cat.org> schrieb im Newsbeitrag 
news:cqCUhZUI.1111865719.6056510.ruerue / bidwell.textdrive.com...
>
> In data 3/26/2005, "Saynatkari" <ruby-ml / magical-cat.org> ha scritto:
>
>>
>>In data 3/26/2005, "(Phil Tomson)" <ptkwt / aracnet.com> ha scritto:
>>
>>>In article <3alo9bF6d2vo8U1 / individual.net>,
>>>Robert Klemme <bob.news / gmx.net> wrote:
>>>>
>>>>
>>>>I'm surprised about "retry", too.  His analysis sounds all very 
>>>>resonable -
>>>>only that "break" does not work for me but "return" does:
>>>>
>>>>>> def my_while(cond)
>>>>>>   break unless cond
>>>>>>   yield
>>>>>>   retry
>>>>>> end
>>>>=> nil
>>>>>> i = 0
>>>>=> 0
>>>>>> my_while i < 10 do
>>>>?>   puts i
>>>>>>   i += 1
>>>>>> end
>>>>0
>>>>1
>>>>2
>>>>3
>>>>4
>>>>5
>>>>6
>>>>7
>>>>8
>>>>9
>>>>LocalJumpError: unexpected break
>>>>        from (irb):2:in `my_while'
>>>>        from (irb):7
>>>
>>>
>>>Hmmm...  break works find for me in 1.8.2.  Time to upgrade?
>>>
>>>
>>>>
>>>>Btw, can anybody think of a way to make my_while return the result of 
>>>>the
>>>>last block evaluation?  It seems impossible because the return occurs 
>>>>before
>>>>the yield...
>>>>
>>>
>>>
>>>How about:
>>>
>>>  def my_while(cond)
>>>    return @ret unless cond
>>>    @ret = yield
>>>    retry
>>>  end
>>
>>@ret may not be defined at that point of evaluation.
>
> Er, defined 'correctly', that is. It'll just be the
> current value of i. Otherwise a novel approach!

What do you mean by "correctly"?  If there's no value you'll get nil which 
is ok if the condition evaluates to false right from the start IMHO.  The 
problem I see with this is rather thread safety (which could be handled by 
thread local variables) and nesting (which is a bit more difficult to cope 
with IMHO; I guess you'll need a stack of values as thread local or such).

Kind regards

    robert