Commit 12e00f92 authored by h2so5's avatar h2so5

implement Class.new with block

parent 88758e36
...@@ -1056,15 +1056,18 @@ mrb_obj_new(mrb_state *mrb, struct RClass *c, int argc, mrb_value *argv) ...@@ -1056,15 +1056,18 @@ mrb_obj_new(mrb_state *mrb, struct RClass *c, int argc, mrb_value *argv)
static mrb_value static mrb_value
mrb_class_new_class(mrb_state *mrb, mrb_value cv) mrb_class_new_class(mrb_state *mrb, mrb_value cv)
{ {
mrb_value super; mrb_value super, blk;
struct RClass *new_class; mrb_value new_class;
if (mrb_get_args(mrb, "|o", &super) == 0) { if (mrb_get_args(mrb, "|o&", &super, &blk) == 0) {
super = mrb_obj_value(mrb->object_class); super = mrb_obj_value(mrb->object_class);
} }
new_class = mrb_class_new(mrb, mrb_class_ptr(super)); new_class = mrb_obj_value(mrb_class_new(mrb, mrb_class_ptr(super)));
mrb_funcall(mrb, super, "inherited", 1, mrb_obj_value(new_class)); if (!mrb_nil_p(blk)) {
return mrb_obj_value(new_class); mrb_funcall_with_block(mrb, new_class, mrb_intern_cstr(mrb, "class_eval"), 0, NULL, blk);
}
mrb_funcall(mrb, super, "inherited", 1, new_class);
return new_class;
} }
mrb_value mrb_value
......
...@@ -9,7 +9,15 @@ assert('Class superclass', '15.2.3.2') do ...@@ -9,7 +9,15 @@ assert('Class superclass', '15.2.3.2') do
assert_equal(Module, Class.superclass) assert_equal(Module, Class.superclass)
end end
# Class#initialize '15.2.3.3.1' is tested in Class#new assert('Class#initialize', '15.2.3.3.1') do
c = Class.new do
def test
:test
end
end.new
assert_equal(c.test, :test)
end
assert('Class#initialize_copy', '15.2.3.3.2') do assert('Class#initialize_copy', '15.2.3.3.2') do
class TestClass class TestClass
......
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