Skip to content

instance_eval, module_eval, class_eval cannot yield to block #7598

@ryannevell

Description

@ryannevell

When instance_eval, module_eval, or class_eval is called with a string containing "yield", it results in a LocalJumpError: yield called out of block exception. This behavior differs from CRuby, as well as Kernel#eval.

Environment Information

jruby 9.4.0.0 (3.1.0) 2022-11-23 95c0ec159f OpenJDK 64-Bit Server VM 17.0.4+8-LTS on 17.0.4+8-LTS +jit [x86_64-darwin]
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-darwin19]
ruby 3.2.0 (2022-12-25 revision a528908271) [x86_64-darwin21]
Darwin --- 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:25:27 PDT 2022; root:xnu-8020.141.5~2/RELEASE_X86_64 x86_64

Test Case

o = Object.new

def o.kernel_eval
  eval('yield')
end

def o.inst_eval
  instance_eval('yield')
end

o.kernel_eval { puts 'Always works' }
o.inst_eval { puts 'Works only in CRuby' }

Expected Behavior
In both versions of CRuby tested (details above), the result is:

Always works
Works only in CRuby

Actual Behavior

In JRuby, the 2nd call fails:

Always works
LocalJumpError: yield called out of block
         (eval) at (eval):1
  instance_eval at org/jruby/RubyBasicObject.java:2511
      inst_eval at test1.rb:8
         <main> at test1.rb:12
... 5 levels...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions