public Method

Object.instance_variables

obj.instance_variables     array

Returns an array of instance variable names for the receiver. Note that simply defining an accessor does not create the corresponding instance variable.

class Fred
  attr_accessor :a1
  def initialize
    @iv = 3
  end
end
Fred.new.instance_variables   #=> ["@iv"]

Source Code

/*
*  call-seq:
*     obj.instance_variables    => array
*  
*  Returns an array of instance variable names for the receiver. Note
*  that simply defining an accessor does not create the corresponding
*  instance variable.
*     
*     class Fred
*       attr_accessor :a1
*       def initialize
*         @iv = 3
*       end
*     end
*     Fred.new.instance_variables   #=> ["@iv"]
*/

VALUE
rb_obj_instance_variables(obj)
   VALUE obj;
{
   VALUE ary;

   ary = rb_ary_new();
   switch (TYPE(obj)) {
     case T_OBJECT:
     case T_CLASS:
     case T_MODULE:
if (ROBJECT(obj)->iv_tbl) {
    st_foreach_safe(ROBJECT(obj)->iv_tbl, ivar_i, ary);
}
break;
     default:
if (!generic_iv_tbl) break;
if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj)) {
    st_table *tbl;

    if (st_lookup(generic_iv_tbl, obj, (st_data_t *)&tbl)) {
	st_foreach_safe(tbl, ivar_i, ary);
    }
}
break;
   }
   return ary;
}
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.