JRuby’ s break just for innermost blocks?

Hello. My name is Piotr S. and I’m new to the list (and to
JRuby). Apologies if the below is a known issue or a FAQ; I tried
googling and searching through the list’s archive, but to no avail.

Now, my problem – a discrepancy in break handling between JRuby 1.1.1
and MRI 1.8.6.p114. After defining the below new Enumerable iterators:

module Enumerable
def every_pair_with_indices
each_with_index do |a, i|
each_with_index do |b, j|
yield [a, b, i, j] if i < j
end
end
end
def every_pair
every_pair_with_indices do |a, b, i, j|
yield [a, b]
end
end
end

JRuby and MRI give me different results:

ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-linux]

[:a,:b,:c,:d].every_pair{|a,b| p [a,b]; break if [a,b] == [:a,:c]}
[:a, :b]
[:a, :c]
=> nil

ruby 1.8.6 (2008-04-22 rev 6555) [i386-jruby1.1.1]

[:a,:b,:c,:d].every_pair{|a,b| p [a,b]; break if [a,b] == [:a,:c]}
[:a, :b]
[:a, :c]
[:b, :c]
[:b, :d]
[:c, :d]
=> [:a, :b, :c, :d]

If I understand the things right, in JRuby the break only breaks
the innermost every_pair loop (i.e., it ‘unwinds’ it into two loops,
and breaks the inner one), while in MRI break actually breaks out of
every_pair.

Is this a known discrepancy, and if so, is there either a way to make
JRuby behave like MRI or a way to rewrite every_pair so it’s possible
to break out of it (in the same manner like out of Enumerable#each)?

– Shot

On 15 May 2008, at 21:25, Shot (Piotr S.) wrote:

Hello. My name is Piotr S. and I’m new to the list (and to
JRuby). Apologies if the below is a known issue or a FAQ; I tried
googling and searching through the list’s archive, but to no avail.

Hi Piotr,

Now, my problem – a discrepancy in break handling between JRuby 1.1.1
and MRI 1.8.6.p114.

I can confirm this is also an issue on jruby trunk. Could you file an
issue with jira?

http://jira.codehaus.org/browse/JRUBY

Thanks,

Damian

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Shot (Piotr S.):

ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-linux]

[:a,:b,:c,:d].every_pair{|a,b| p [a,b]; break if [a,b] == [:a,:c]}
[:a, :b]
[:a, :c]
=> nil

ruby 1.8.6 (2008-04-22 rev 6555) [i386-jruby1.1.1]

[:a,:b,:c,:d].every_pair{|a,b| p [a,b]; break if [a,b] == [:a,:c]}
[:a, :b]
[:a, :c]
[:b, :c]
[:b, :d]
[:c, :d]
=> [:a, :b, :c, :d]

Is this a known discrepancy, and if so, is there
either a way to make JRuby behave like MRI

FWIW: In my limited set of use cases, I worked around this
by raising a custom exception and rescuing it after the loop.

– Shot

On 16 May 2008, at 09:01, Shot (Piotr S.) wrote:

FWIW: In my limited set of use cases, I worked around this
by raising a custom exception and rescuing it after the loop.

– Shot

Thanks for posting a workaround, and for submitting a bug report.
JRuby seems to have a few issues with break, having tried a few
reductions.

Damian


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email