remove_instance_variable(p1)
public
Removes the named instance variable from obj, returning that
variable’s value.
class Dummy
attr_reader :var
def initialize
@var = 99
end
def remove
remove_instance_variable(:@var)
end
end
d = Dummy.new
d.var
d.remove
d.var
Show source
VALUE
rb_obj_remove_instance_variable(VALUE obj, VALUE name)
{
VALUE val = Qnil;
const ID id = rb_check_id(&name);
st_data_t n, v;
struct st_table *iv_index_tbl;
st_data_t index;
rb_check_frozen(obj);
if (!id) {
if (rb_is_instance_name(name)) {
rb_name_error_str(name, "instance variable %"PRIsVALUE" not defined",
name);
}
else {
rb_name_error_str(name, "`%"PRIsVALUE"' is not allowed as an instance variable name",
QUOTE(name));
}
}
if (!rb_is_instance_id(id)) {
rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
QUOTE_ID(id));
}
if (SPECIAL_CONST_P(obj)) goto generic;
switch (BUILTIN_TYPE(obj)) {
case T_OBJECT:
iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
if (!iv_index_tbl) break;
if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) break;
if (ROBJECT_NUMIV(obj) <= (long)index) break;
val = ROBJECT_IVPTR(obj)[index];
if (val != Qundef) {
ROBJECT_IVPTR(obj)[index] = Qundef;
return val;
}
break;
case T_CLASS:
case T_MODULE:
n = id;
if (RCLASS_IV_TBL(obj) && st_delete(RCLASS_IV_TBL(obj), &n, &v)) {
return (VALUE)v;
}
break;
default:
generic:
if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj)) {
v = val;
if (generic_ivar_remove(obj, (st_data_t)id, &v)) {
return (VALUE)v;
}
}
break;
}
rb_name_error(id, "instance variable %"PRIsVALUE" not defined", QUOTE_ID(id));
UNREACHABLE;
}