Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Object#remove_instance_variable
(::interned variable) -> untyped
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)
{
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) {
VALUE val = rb_ivar_delete(obj, id, Qundef);
if (!UNDEF_P(val)) return val;
}
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. The name can be passed as a symbol or as a string.
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.3 · 3.2 · 3.1 · 3.0