public Method

Object.remove_instance_variable(p1)

obj.remove_instance_variable(symbol)     obj

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      #=> 99
d.remove   #=> 99
d.var      #=> nil

Source Code

/*
*  call-seq:
*     obj.remove_instance_variable(symbol)    => obj
*  
*  Removes the named instance variable from <i>obj</i>, 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      #=> 99
*     d.remove   #=> 99
*     d.var      #=> nil
*/

VALUE
rb_obj_remove_instance_variable(obj, name)
   VALUE obj, name;
{
   VALUE val = Qnil;
   ID id = rb_to_id(name);

   if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: can't modify instance variable");
   if (OBJ_FROZEN(obj)) rb_error_frozen("object");
   if (!rb_is_instance_id(id)) {
rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
   }

   switch (TYPE(obj)) {
     case T_OBJECT:
     case T_CLASS:
     case T_MODULE:
if (ROBJECT(obj)->iv_tbl && st_delete(ROBJECT(obj)->iv_tbl, (st_data_t*)&id, &val)) {
    return val;
}
break;
     default:
if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj)) {
    if (generic_ivar_remove(obj, id, &val)) {
	return val;
    }
}
break;
   }
   rb_name_error(id, "instance variable %s not defined", rb_id2name(id));
   return Qnil;		/* not reached */
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.