End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Object#remove_instance_variable
obj.remove_instance_variable(symbol) → obj
obj.remove_instance_variable(string) → obj
object.c
View on GitHub ↗
VALUE
rb_obj_remove_instance_variable(VALUE obj, VALUE name)
{
VALUE val = Qundef;
const ID id = id_for_var(obj, name, an, instance);
// Frozen check comes here because it's expected that we raise a
// NameError (from the id_for_var check) before we raise a FrozenError
rb_check_frozen(obj);
if (!id) {
goto not_defined;
}
rb_shape_t * shape = rb_shape_get_shape(obj);
switch (BUILTIN_TYPE(obj)) {
case T_CLASS:
case T_MODULE:
IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id);
rb_shape_transition_shape_remove_ivar(obj, id, shape, &val);
break;
case T_OBJECT: {
if (rb_shape_obj_too_complex(obj)) {
if (rb_id_table_lookup(ROBJECT_IV_HASH(obj), id, &val)) {
rb_id_table_delete(ROBJECT_IV_HASH(obj), id);
}
}
else {
rb_shape_transition_shape_remove_ivar(obj, id, shape, &val);
}
break;
}
default: {
rb_shape_transition_shape_remove_ivar(obj, id, shape, &val);
break;
}
}
if (val != Qundef) {
return val;
}
not_defined:
rb_name_err_raise("instance variable %1$s not defined",
obj, name);
UNREACHABLE_RETURN(Qnil);
}
Removes the named instance variable from obj, returning that variable’s value. String arguments are converted to symbols.
Example
class Dummy attr_reader :var def initialize @var = 99 end def remove remove_instance_variable(:@var) end end d = Dummy.new d.var #=> 99 d.remove #=> 99 d.var #=> nil
See also
#public_send
#respond_to?
#public_methods
#respond_to_missing?
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0