Strange `return` behavior when switching from matz ruby ​​to jruby

Why this snippet:

def dump_dump
    get_dump = lambda do
        return 1 if $n
        $n = true
        module_exec &get_dump
        2
    end
    p get_dump[]
end

Module.new do
    module_exec &method(:dump_dump)
end

print 2in ruby ​​2.0.0p481 (2014-05-08) [x64-mingw32]
but 1in jruby 1.7.15 (1.9.3p392) 2014-09-03 82b5cc3 on a Java HotSpot (TM) virtual machine with a 64-bit server 1.7.0_67-b01 + jit [Windows 8-amd64]?

I would like to understand the problem.

UPD: should I report it somewhere?

+4
source share
2 answers

I always got the impression that returninside the block was undefined. Can you use nextinstead?

For example, Rubinius also has this problem, but much more explicit:

[1].map(&lambda { |n| return -1 })
LocalJumpError: unexpected return

, next :

rbx-head :003 > [1].map(&lambda { |n| next -1 })
 => [-1] 

, return , Procs lambdas - . next break - , , .

return Ruby, rubyspec , return, .

https://github.com/rubyspec/rubyspec/blob/master/language/return_spec.rb#L184

+2

A 'return' . , , jruby -.

, - , , , , :

Module.new do
  test = lambda do
    return
  end
  module_exec &test
  puts 'after'
end

mri "after", jruby .

... lambda ( &):

Module.new do
  test = lambda do
    return
  end
  module_exec { test[] }
  puts 'after'
end

mri, jruby print 'after'...

0

All Articles