From: Sean O'Halpin Date: 2009-04-20T06:53:39+09:00 Subject: Re: ruby1.9 and the retry keyword On Sun, Apr 19, 2009 at 7:17 PM, Yoann Guillot wrote: > On Mon, Apr 20, 2009 at 02:10:33AM +0900, James Gray wrote: >> On Apr 19, 2009, at 12:01 PM, Christopher Dicely wrote: >> >>> That mimics the example (where the retry is unconditional), but >>> clearly isn't a general solution; >> >> I'll be surprised if more complex examples can't be similarly converted >> fairly easily.  That was kind of my point.  I meant the code to say, take >> a step back and think what is this trying to do? >> > > My code walks an array, and may change the current element, which is allowed > without breaking the iteration. > > I have also a special case where i have to change a whole subsection of the > array, this was the case where i used 'retry' > > eg > ary.each { |e| >        case e >        when foo >                ary[ary.index(e)] = foo(e) >        when bar >        when ... >        when baz >                ary[ary.index(e), 12] = [flublu] >                retry >        end > } > > Sure, i could use map or many other ways, my code is quite ugly anyway. > I was just surprised by the change of behavior. > > I don't see the point in removing capabilities from the language (restarting > an iteration from within), but I can sure live with it. > > -- > Yoann > > You could do this I guess: ary = [:foo, :bar, :baz, :foo] def foo(e) :bom end class Retry < StandardError end begin ary.each { |e| case e when :foo ary[ary.index(e)] = foo(e) when :bom ary[ary.index(e)] = :foobom # to show retry when :baz ary[ary.index(e), 12] = [:flublu] raise Retry end } rescue Retry retry end p ary __END__ [:foobom, :bar, :flublu] but as you point out, this is not restarting the iteration from within. Regards, Sean