Method: Object#remove_instance_variable

Defined in:
variable.c

#remove_instance_variable(symbol) ⇒ Object #remove_instance_variable(string) ⇒ Object

Removes the named instance variable from obj, returning that variable’s value. The name can be passed as a symbol or as a string.

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

Overloads:

  • #remove_instance_variable(symbol) ⇒ Object

    Returns:

  • #remove_instance_variable(string) ⇒ Object

    Returns:



2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
# File 'variable.c', line 2260

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);
}