History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: JRUBY-1281
Type: Bug Bug
Status: Open Open
Priority: Minor Minor
Assignee: Unassigned
Reporter: Ken Bloom
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
JRuby

"break" from recursive "each" doesn't work

Created: 19/Aug/07 06:42 PM   Updated: 17/Mar/08 06:13 PM
Component/s: Interpreter
Affects Version/s: JRuby 1.0.0
Fix Version/s: JRuby 1.1+

Time Tracking:
Not Specified

Environment: JRuby 1.0.0-2 on Debian/sid


 Description  « Hide
Charles Oliver Nutter was discussing JRuby's "break" semantics on user@groovy.codehaus.org, when he said the following:

> Generally break would terminate the nearest enclosing loop. Since times
> and each are implemented in terms of a loop, they would terminate. If
> the closure passed to them contained a call to some other looping
> structure, that looping structure would terminate.

So I decided that I'd try a recursive version, since it should make no difference to the user whether each is implemented recursively or iteratively. As a result, I discovered a difference between JRuby and MRI

def my_each array, index, &block
  return self if index==array.length
  block.call(array[index])
  my_each array, index+1, &block
  puts array[index]
end


my_each([1,2,3,4],0) do |x|
  break if x==3
  puts x
end

In MRI, this prints:

1
2

In JRuby, it breaks:

1
2
-:3:in `my_each': break from proc-closure (LocalJumpError)
	from -:4:in `my_each'
	from -:12

On the other hand, the following also breaks in MRI

def my_each array, index, &block
  return self if index==array.length
  block.call(array[index])
  my_each array, index+1, &block
  puts array[index]
end

x=Proc.new do |x|
  break if x==3
  puts x
end

my_each([1,2,3,4],0, &x)

in MRI:

1
2
-:9:in `call': break from proc-closure (LocalJumpError)
	from -:3:in `my_each'
	from -:4:in `my_each'
	from -:13

but it breaks differently in JRuby:

1
2
-:3:in `my_each': break from proc-closure (LocalJumpError)
	from -:4:in `my_each'
	from -:13

Apparently, in MRI, a break call breaks back to the lexical scope where the proc was declared if you didn't detatch the declaration from its use.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Charles Oliver Nutter - 19/Aug/07 09:10 PM
Thanks for this report Ken. We've chased around various inconsistencies in our implementation of Ruby's non-local flow control, and you have stumbled upon another difference. We'll see about getting it fixed for 1.1, and perhaps 1.0.2 if it's released before then.

Charles Oliver Nutter - 19/Oct/07 10:29 PM
This is related to the fact that we have two possible events to track for non-local flow control like break: Java JumpException.Xs and ruby LocalJumpErrors. As part of 1.1, I want to unify the two. I don't know if we'll have time, but it should resolve all remaining non-local flow control issues.

Charles Oliver Nutter - 15/Feb/08 12:58 PM
Punting issues from 1.1 RC2 to 1.1 final.

Thomas E Enebo - 17/Mar/08 06:13 PM
Not enough time to get to this. Bumping.