Skip to content

Commit

Permalink
Merge pull request #677 from skandhas/pr-add-Module-remove_method
Browse files Browse the repository at this point in the history
Add Module#remove_method for mruby
  • Loading branch information
matz committed Dec 29, 2012
2 parents c836187 + 9219c85 commit 50e3a53
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/class.c
Expand Up @@ -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 <code>Module.undef_method</code>.
*/

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)
{
Expand Down Expand Up @@ -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());
Expand Down
19 changes: 19 additions & 0 deletions test/t/module.rb
Expand Up @@ -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

Expand Down

0 comments on commit 50e3a53

Please sign in to comment.