Commit e75e2083 authored by Kouhei Sutou's avatar Kouhei Sutou

Fix a bug that class variable can't be referenced from class method

Class method defined in singleton class should be evaluated in class
context not singleton class context.

fix #2515
parent 39488f0b
...@@ -2175,6 +2175,15 @@ RETRY_TRY_BLOCK: ...@@ -2175,6 +2175,15 @@ RETRY_TRY_BLOCK:
} }
else { else {
p = mrb_proc_new(mrb, irep->reps[GETARG_b(i)]); p = mrb_proc_new(mrb, irep->reps[GETARG_b(i)]);
if (c & OP_L_METHOD) {
if (p->target_class->tt == MRB_TT_SCLASS) {
mrb_value klass;
klass = mrb_obj_iv_get(mrb,
(struct RObject *)p->target_class,
mrb_intern_lit(mrb, "__attached__"));
p->target_class = mrb_class_ptr(klass);
}
}
} }
if (c & OP_L_STRICT) p->flags |= MRB_PROC_STRICT; if (c & OP_L_STRICT) p->flags |= MRB_PROC_STRICT;
regs[GETARG_A(i)] = mrb_obj_value(p); regs[GETARG_A(i)] = mrb_obj_value(p);
......
...@@ -370,3 +370,16 @@ assert('clone Class') do ...@@ -370,3 +370,16 @@ assert('clone Class') do
Foo.clone.new.func Foo.clone.new.func
end end
assert('class variable and class << self style class method') do
class ClassVariableTest
@@class_variable = "value"
class << self
def class_variable
@@class_variable
end
end
end
assert_equal("value", ClassVariableTest.class_variable)
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment