ruby1.9 and the retry keyword

Y

Yoann Guillot

Hi

It seems that the 'retry' keyword is not supported anymore in ruby1.9 to
restart a running iteration from the beginning.

Is there a way to achieve the old behavior, preferably in a 1.8 compatible
way ?

$ ruby1.8 -ve '[1, 2].each { puts "bla" ; retry }'
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
bla
bla
bla
^C

$ ruby1.9 -ve '[1, 2].each { puts "bla" ; retry }'
ruby 1.9.2dev (2009-04-19) [i686-linux]
-e:1: Invalid retry
-e: compile error (SyntaxError)
 
R

Robert Klemme

It seems that the 'retry' keyword is not supported anymore in ruby1.9 to
restart a running iteration from the beginning.

This cannot be, "retry" works in 1.9.1:

[robert@ora01 ~]$ irb19
irb(main):001:0> i = 5
=> 5
irb(main):002:0> begin
irb(main):003:1* puts i
irb(main):004:1> raise "foo"
irb(main):005:1> rescue Exception
irb(main):006:1> i -= 1
irb(main):007:1> retry if i > 0
irb(main):008:1> end
5
4
3
2
1
=> nil
irb(main):009:0> RUBY_VERSION
=> "1.9.1"
irb(main):010:0>
Is there a way to achieve the old behavior, preferably in a 1.8 compatible
way ?

$ ruby1.8 -ve '[1, 2].each { puts "bla" ; retry }'
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
bla
bla
bla
^C

"retry" without "rescue" seems pointless to me. As far as I can see
"retry"'s main purpose is repetition in case of exceptions. The use
that you demonstrate looks like a untypical and probably unwanted idiom
which accidentally happened to work in 1.8.* to me.
$ ruby1.9 -ve '[1, 2].each { puts "bla" ; retry }'
ruby 1.9.2dev (2009-04-19) [i686-linux]
-e:1: Invalid retry
-e: compile error (SyntaxError)

Apparently the _syntax_ was changed to allow "retry" only in "rescue"
clauses - which makes perfectly sense for me. I am not the definitive
source but to me it looks like 1.9 fixes a misbehavior of 1.8 - if
that's the case I would not want to reintroduce this just to make 1.9
more compatible to 1.8. In fact, 1.9 purposefully *does* have some
incompatibilities.

Kind regards

robert
 
L

Leo

It seems that the 'retry' keyword is not supported anymore in ruby1.9 to
restart a running iteration from the beginning.

Are you sure you don't want to use 'redo'?

$ ruby -ve '[1, 2].each { puts "bla" ; redo }'
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-cygwin]
bla
bla
...
 
J

James Gray

It seems that the 'retry' keyword is not supported anymore in
ruby1.9 to
restart a running iteration from the beginning.

As Robert said, it's no longer supported in iteration. It still works
in a rescue clause.
Is there a way to achieve the old behavior, preferably in a 1.8
compatible way ?

You bet.
$ ruby1.8 -ve '[1, 2].each { puts "bla" ; retry }'

loop do
puts "bla"
end

James Edward Gray II
 
C

Christopher Dicely

It seems that the 'retry' keyword is not supported anymore in ruby1.9 to
restart a running iteration from the beginning.

As Robert said, it's no longer supported in iteration. =C2=A0It still wor= ks in a
rescue clause.
Is there a way to achieve the old behavior, preferably in a 1.8 compatib= le
way ?

You bet.
$ ruby1.8 -ve '[1, 2].each { puts "bla" ; retry }'

loop do
=C2=A0puts "bla"
end

That mimics the example (where the retry is unconditional), but
clearly isn't a general solution; If "redo" works on 1.9 (which is
similar to retry, but different in conditions not relevant to the
question), that may be the answer.
 
J

James Gray

Is there a way to achieve the old behavior, preferably in a 1.8 =20
compatible
way ?

You bet.
$ ruby1.8 -ve '[1, 2].each { puts "bla" ; retry }'

loop do
puts "bla"
end

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 =20
converted fairly easily. That was kind of my point. I meant the code =20=

to say, take a step back and think what is this trying to do=85

James Edward Gray II
 
Y

Yoann Guillot

It seems that the 'retry' keyword is not supported anymore in ruby1.9 to
restart a running iteration from the beginning.

Are you sure you don't want to use 'redo'?

$ ruby -ve '[1, 2].each { puts "bla" ; redo }'
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-cygwin]
bla
bla
...

redo reruns the current iteration, retry restarted the whole iteration

[1, 2].each { |i| puts i ; redo if i == 2 }
1 2 2 2 2 2 2
[1, 2].each { |i| puts i ; retry if i == 2 }
 
J

James Gray

It seems that the 'retry' keyword is not supported anymore in
ruby1.9 to
restart a running iteration from the beginning.

Are you sure you don't want to use 'redo'?

$ ruby -ve '[1, 2].each { puts "bla" ; redo }'
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-cygwin]
bla
bla
...

redo reruns the current iteration, retry restarted the whole iteration

[1, 2].each { |i| puts i ; redo if i == 2 }
1 2 2 2 2 2 2
[1, 2].each { |i| puts i ; retry if i == 2 }
1 2 1 2 1 2 1

$ ruby_dev -ve '[1, 2].cycle { |i| puts i }'
ruby 1.9.2dev (2009-04-13) [i386-darwin9.6.0]
1
2
1
2
1
2
1
2
-e:1:in `write': Interrupt
from -e:1:in `puts'
from -e:1:in `puts'
from -e:1:in `block in <main>'
from -e:1:in `cycle'
from -e:1:in `<main>'

James Edward Gray II
 
Y

Yoann Guillot

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.
 
J

James Gray

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.

Yeah, for a case like this, I would probably just resort to tracking
an index in a variable and using a boring while loop. 90% of the time
it's wrong to track your own index in Ruby, but I think you're
squarely in the special 10% here.

Really, you should already be using each_with_index() above because
your repeated calls to index() are inefficient for big data sets
(rewalking possibly large portions of the Array) and they break if the
data set contains duplicate entries.

Plus, I'm just not comfortable relying on the iterators to do the
right thing while I edit the collection out from underneath them.
Obviously it was working for you, but I'm not confident enough to
trust it under all cases.
I don't see the point in removing capabilities from the language
(restarting an iteration from within), but I can sure live with it.

It was done because retry had some nasty side effects and was causing
performance issues:

http://markmail.org/message/laisvqelyjmgh56p#query:ruby core retry
%20remove+page:1+mid:fxoxlphjdndatp4m+state:results

James Edward Gray II
 
S

Sean O'Halpin

I'll be surprised if more complex examples can't be similarly converted
fairly easily. =A0That was kind of my point. =A0I 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 allo= wed
without breaking the iteration.

I have also a special case where i have to change a whole subsection of t= he
array, this was the case where i used 'retry'

eg
ary.each { |e|
=A0 =A0 =A0 =A0case e
=A0 =A0 =A0 =A0when foo
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ary[ary.index(e)] =3D foo(e)
=A0 =A0 =A0 =A0when bar
=A0 =A0 =A0 =A0when ...
=A0 =A0 =A0 =A0when baz
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ary[ary.index(e), 12] =3D [flublu]
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0retry
=A0 =A0 =A0 =A0end
}

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 (restart= ing
an iteration from within), but I can sure live with it.

You could do this I guess:

ary =3D [: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)] =3D foo(e)
when :bom
ary[ary.index(e)] =3D :foobom # to show retry
when :baz
ary[ary.index(e), 12] =3D [: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
 
B

Brian Candler

Or using throw/catch. You can also factor out the pattern, for example:

def retryable
loop do
catch:)retry) do
yield
return
end
end
end

ary = [:foo, :bar, :baz, :foo]

def foo(e)
:bom
end

retryable do
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]
throw :retry
end
}
end
p ary
__END__
[:foobom, :bar, :flublu]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top