"break" from recursive "each" doesn't work
------------------------------------------
Key: JRUBY-1281
URL: http://jira.codehaus.org/browse/JRUBY-1281
Project: JRuby
Issue Type: Bug
Components: Interpreter
Affects Versions: JRuby 1.0.0
Environment: JRuby 1.0.0-2 on Debian/sid
Reporter: Ken Bloom
Priority: Minor
Charles Oliver Nutter was discussing JRuby's "break" semantics on [EMAIL
PROTECTED], 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
{code}
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
{code}
In MRI, this prints:
{code}
1
2
{code}
In JRuby, it breaks:
{code}
1
2
-:3:in `my_each': break from proc-closure (LocalJumpError)
from -:4:in `my_each'
from -:12
{code}
On the other hand, the following also breaks in MRI
{code}
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)
{code}
in MRI:
{code}
1
2
-:9:in `call': break from proc-closure (LocalJumpError)
from -:3:in `my_each'
from -:4:in `my_each'
from -:13
{code}
but it breaks differently in JRuby:
{code}
1
2
-:3:in `my_each': break from proc-closure (LocalJumpError)
from -:4:in `my_each'
from -:13
{code}
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.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email