From 779a73e7de17a970fab5418917b5b952293c85d0 Mon Sep 17 00:00:00 2001 From: skandhas Date: Sat, 29 Dec 2012 14:22:35 +0800 Subject: [PATCH 1/2] add mrb_mod_remove_method/remove_method --- src/class.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/class.c b/src/class.c index db334cf3aa..ace34eb348 100644 --- a/src/class.c +++ b/src/class.c @@ -1595,6 +1595,48 @@ mrb_mod_remove_cvar(mrb_state *mrb, mrb_value mod) return mrb_nil_value(); } +static void +remove_method(mrb_state *mrb, struct RClass *c, mrb_sym mid) +{ + khash_t(mt) *h = c->mt; + khiter_t k; + + if (h) { + k = kh_get(mt, h, mid); + if (k != kh_end(h)) { + kh_del(mt, h, k); + return; + } + } + + mrb_name_error(mrb, mid, "method `%s' not defined in %s", + mrb_sym2name(mrb, mid), mrb_class_name(mrb, c)); +} + +/* 15.2.2.4.41 */ +/* + * call-seq: + * remove_method(symbol) -> self + * + * Removes the method identified by _symbol_ from the current + * class. For an example, see Module.undef_method. + */ + +mrb_value +mrb_mod_remove_method(mrb_state *mrb, mrb_value mod) +{ + struct RClass *c = mrb_class_ptr(mod); + int argc; + mrb_value *argv; + + mrb_get_args(mrb, "*", &argv, &argc); + while (argc--) { + remove_method(mrb, c, mrb_symbol(*argv)); + argv++; + } + return mod; +} + static void check_const_name(mrb_state *mrb, mrb_sym id) { @@ -1710,6 +1752,7 @@ mrb_init_class(mrb_state *mrb) mrb_define_method(mrb, mod, "instance_methods", mrb_mod_instance_methods, ARGS_ANY()); /* 15.2.2.4.33 */ mrb_define_method(mrb, mod, "module_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.35 */ mrb_define_method(mrb, mod, "remove_class_variable", mrb_mod_remove_cvar, ARGS_REQ(1)); /* 15.2.2.4.39 */ + mrb_define_method(mrb, mod, "remove_method", mrb_mod_remove_method, ARGS_ANY()); /* 15.2.2.4.41 */ mrb_define_method(mrb, mod, "to_s", mrb_mod_to_s, ARGS_NONE()); mrb_define_method(mrb, mod, "inspect", mrb_mod_to_s, ARGS_NONE()); From 9219c856a8586fbda74fec526303d19c70290484 Mon Sep 17 00:00:00 2001 From: skandhas Date: Sat, 29 Dec 2012 14:44:28 +0800 Subject: [PATCH 2/2] add test for Module#remove_method --- test/t/module.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/t/module.rb b/test/t/module.rb index deacb33090..bf9626c2d2 100644 --- a/test/t/module.rb +++ b/test/t/module.rb @@ -217,6 +217,25 @@ class Test4RemoveClassVariable not Test4RemoveClassVariable.class_variables.include? :@@cv end +assert('Module#remove_method', '15.2.2.4.41') do + module Test4RemoveMethod + class Parent + def hello + end + end + + class Child < Parent + def hello + end + end + end + + Test4RemoveMethod::Child.class_eval{ remove_method :hello } + + Test4RemoveMethod::Child.instance_methods.include? :hello and + not Test4RemoveMethod::Child.instance_methods(false).include? :hello +end + # Not ISO specified