suetanvil · GitHub

I've noticed that this particular wierd corner case behaves differently between MRuby (3.2.0) and CRuby (3.2.1 and 3.0..6). In this example, we capture the binding from an instance_exec call on the main object:

MAIN = self
module S
  @@repl_session = MAIN.instance_exec{ binding() }
  def self.session_eval(text)
    return @@repl_session.eval(text)
  end
end
S.session_eval 'def self.bar() puts "bar!"; end'
S.session_eval 'def foo() puts "foo!"; end'
bar()
foo()   # fails on mruby

On CRuby, both foo and bar are defined. However, on MRuby, only bar (defined with self.bar) is visible in the toplevel object.

Oddly enough, if I define @@repl_session in the toplevel:

...
REPL_SESSION = MAIN.instance_exec{ binding() }
module S
  def self.session_eval(text)
    return REPL_SESSION.eval(text)
  end
end
...

everything works as expected. My suspicion is that the current binding isn't getting self correctly, but I could be wrong.

Read the original on github.com ↗