Kernel#instance_eval should define singleton methods; fix
MODIFY
mrbgems/mruby-eval/src/eval.c
mrbgems/mruby-eval/src/eval.c
@@ -325,6 +325,7 @@ f_instance_eval(mrb_state *mrb, mrb_value self)
proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv));
mrb_assert(!MRB_PROC_CFUNC_P(proc));
+ mrb->c->ci->target_class = mrb_class_ptr(cv);
return exec_irep(mrb, self, proc);
}
else {

MODIFY
mrbgems/mruby-eval/test/eval.rb
mrbgems/mruby-eval/test/eval.rb
@@ -100,7 +100,22 @@ assert('Object#instance_eval with begin-rescue-ensure execution order') do
assert_equal([:enter_raise_hell, :begin, :rescue, :ensure], hell_raiser.raise_hell)
end
-assert('Kernel.#eval(strinng) Issue #4021') do
+assert('Kernel#instance_eval() to define singleton methods Issue #3141') do
+ foo_class = Class.new do
+ def bar(x)
+ instance_eval "def baz; #{x}; end"
+ end
+ end
+
+ f1 = foo_class.new
+ f2 = foo_class.new
+ f1.bar 1
+ f2.bar 2
+ assert_equal(1){f1.baz}
+ assert_equal(2){f2.baz}
+end
+
+assert('Kernel.#eval(string) Issue #4021') do
assert_equal('FOO') { (eval <<'EOS').call }
foo = "FOO"
Proc.new { foo }

Kernel#instance_eval should define singleton methods; fix #4069