Permalink
Browse files

Module#define_method supports proc argument

  • Loading branch information...
ksss committed Oct 12, 2016
1 parent 9d3167c commit efe4f30b3982e8ff85a9142933955d6fa0bdebc7
Showing with 25 additions and 1 deletion.
  1. +13 −1 src/class.c
  2. +12 −0 test/t/module.rb
View
@@ -1773,9 +1773,21 @@ mod_define_method(mrb_state *mrb, mrb_value self)
struct RClass *c = mrb_class_ptr(self);
struct RProc *p;
mrb_sym mid;
mrb_value proc = mrb_undef_value();
mrb_value blk;
mrb_get_args(mrb, "n&", &mid, &blk);
mrb_get_args(mrb, "n|o&", &mid, &proc, &blk);
switch (mrb_type(proc)) {
case MRB_TT_PROC:
blk = proc;
break;
case MRB_TT_UNDEF:
/* ignored */
break;
default:
mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %S (expected Proc)", mrb_obj_value(mrb_obj_class(mrb, proc)));
break;
}
if (mrb_nil_p(blk)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
}
View
@@ -494,6 +494,18 @@ class GrandChild < Child
# Not ISO specified
assert('Module#define_method') do
c = Class.new {
define_method(:m1) { :ok }
define_method(:m2, Proc.new { :ok })
}
assert_equal c.new.m1, :ok
assert_equal c.new.m2, :ok
assert_raise(TypeError) do
Class.new { define_method(:n1, nil) }
end
end
# @!group prepend
assert('Module#prepend') do
module M0

0 comments on commit efe4f30

Please sign in to comment.