# JRubyâ€™ s break just for innermost blocks?

**URL:** <https://www.ruby-forum.com/t/jrubya-s-break-just-for-innermost-blocks/137742>\
**Category:** JRuby\
**Created:** [May 15, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/jrubya-s-break-just-for-innermost-blocks/137742 "2008-05-15T22:19:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [May 15, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/jrubya-s-break-just-for-innermost-blocks/137742/1 "2008-05-15T22:19:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [May 15, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/jrubya-s-break-just-for-innermost-blocks/137742/2 "2008-05-15T22:51:54Z")

</div>

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](http://jira.codehaus.org/browse/JRUBY)

Thanks,

## Damian

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [May 16, 2008, 9:55am UTC](https://www.ruby-forum.com/t/jrubya-s-break-just-for-innermost-blocks/137742/3 "2008-05-16T09:55:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [May 16, 2008, 10:15am UTC](https://www.ruby-forum.com/t/jrubya-s-break-just-for-innermost-blocks/137742/4 "2008-05-16T10:15:04Z")

</div>

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

```
