Skip to content

Commit

Permalink
Merge pull request #671 from skandhas/pr-add-Module_remove_class_vari…
Browse files Browse the repository at this point in the history
…able

Add Module#remove_class_variable for mruby.
  • Loading branch information
matz committed Dec 25, 2012
2 parents 74554e0 + 3ffe8fe commit c0af042
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/class.c
Expand Up @@ -1545,6 +1545,56 @@ mrb_mod_cvar_set(mrb_state *mrb, mrb_value mod)
return value;
}

/* 15.2.2.4.39 */
/*
* call-seq:
* remove_class_variable(sym) -> obj
*
* Removes the definition of the <i>sym</i>, returning that
* constant's value.
*
* class Dummy
* @@var = 99
* puts @@var
* p class_variables
* remove_class_variable(:@@var)
* p class_variables
* end
*
* <em>produces:</em>
*
* 99
* [:@@var]
* []
*/

mrb_value
mrb_mod_remove_cvar(mrb_state *mrb, mrb_value mod)
{
mrb_value sym, val;
mrb_sym id;

mrb_get_args(mrb, "o", &sym);

id = mrb_sym_value(mrb,sym);
check_cv_name(mrb, id);

val = mrb_iv_remove(mrb, mod, id);

if (!mrb_undef_p(val)) return val;

if (mrb_cv_defined(mrb, mod, id)){
mrb_name_error(mrb, id, "cannot remove %s for %s",
mrb_sym2name(mrb, id), mrb_class_name(mrb, mrb_class_ptr(mod)));
}

mrb_name_error(mrb, id, "class variable %s not defined for %s",
mrb_sym2name(mrb, id), mrb_class_name(mrb, mrb_class_ptr(mod)));

/* not reached */
return mrb_nil_value();
}

static void
check_const_name(mrb_state *mrb, mrb_sym id)
{
Expand Down Expand Up @@ -1659,6 +1709,7 @@ mrb_init_class(mrb_state *mrb)
mrb_define_method(mrb, mod, "included_modules", mrb_mod_included_modules, ARGS_NONE()); /* 15.2.2.4.30 */
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, "to_s", mrb_mod_to_s, ARGS_NONE());
mrb_define_method(mrb, mod, "inspect", mrb_mod_to_s, ARGS_NONE());
Expand Down
10 changes: 10 additions & 0 deletions test/t/module.rb
Expand Up @@ -208,6 +208,16 @@ module Test4ModuleEval
Test4ModuleEval.module_eval{ @b } == 12
end

assert('Module#remove_class_variable', '15.2.2.4.39') do
class Test4RemoveClassVariable
@@cv = 99
end

Test4RemoveClassVariable.remove_class_variable(:@@cv) == 99 and
not Test4RemoveClassVariable.class_variables.include? :@@cv
end


# Not ISO specified

assert('Module#to_s') do
Expand Down

0 comments on commit c0af042

Please sign in to comment.