# Instance Variable Names Available from Class?

**URL:** <https://www.ruby-forum.com/t/instance-variable-names-available-from-class/161455>\
**Category:** Ruby\
**Created:** [February 26, 2009, 5:44pm UTC](https://www.ruby-forum.com/t/instance-variable-names-available-from-class/161455 "2009-02-26T17:44:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 26, 2009, 5:44pm UTC](https://www.ruby-forum.com/t/instance-variable-names-available-from-class/161455/1 "2009-02-26T17:44:24Z")

</div>

In metaprogramming, I know how to get the instance variable names from  
an Object (through instance\_variables). Is there a way to get them from  
a Class?

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 26, 2009, 6:28pm UTC](https://www.ruby-forum.com/t/instance-variable-names-available-from-class/161455/2 "2009-02-26T18:28:27Z")

</div>

On Thu, Feb 26, 2009 at 10:13 PM, Chris Gardner  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In metaprogramming, I know how to get the instance variable names from  
> an Object (through instance\_variables). Is there a way to get them from  
> a Class?

Hello Chris, before answering your question, let me ask: does  
#instance\_variables really work the way you think it does?  
Consider:  
$: irb  
01\> class Foo  
02\> def initialize  
03\> @bar = ‘bar’  
04\> end  
05\> def baz=(arg)  
06\> @baz = arg  
07\> end  
08\> end  
 → nil  
09\> Foo.new.instance\_variables  
 → [:@bar]

Why isn’t @baz listed?  
What would you have to do before it would be listed?

Ponder on that, then read over this previous thread:  
[http://www.ruby-forum.com/topic/107601](http://www.ruby-forum.com/topic/107601)

Hope that gives you some new perspectives (and eventually, the answer  
to your original question!)

Cheers,  
lasitha

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 26, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/instance-variable-names-available-from-class/161455/3 "2009-02-26T20:51:01Z")

</div>

Chris Gardner wrote:

> In metaprogramming, I know how to get the instance variable names from  
> an Object (through instance\_variables). Is there a way to get them from  
> a Class?

A class _is_ an object, with its own instance variables, and you can see  
them in the same way:

class Foo  
@bar = 123

def self.flurble  
@bar  
end  
end

p Foo.instance\_variables # ["@bar"]

p Foo.flurble # 123

Or did you mean something else?

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 26, 2009, 9:00pm UTC](https://www.ruby-forum.com/t/instance-variable-names-available-from-class/161455/4 "2009-02-26T21:00:46Z")

</div>

What I mean is that I would like to ask a class for the names of  
instance variables of its objects, which may have yet to be  
instantiated.

class Foo  
def intialize(bar, baz)  
@bar = bar  
@baz = baz  
end  
end

I was trying to ask class Foo for the names @bar and @baz. I’ve found  
out that these are accessible only through Foo instances.

I’m a Ruby newbie and am just today learning about Ruby’s approach to  
metaprogramming.

Brian C. wrote:

> Chris Gardner wrote:
> 
> > In metaprogramming, I know how to get the instance variable names from  
> > an Object (through instance\_variables). Is there a way to get them from  
> > a Class?
> 
> A class _is_ an object, with its own instance variables, and you can see  
> them in the same way:
> 
> class Foo  
> @bar = 123
> 
> def self.flurble  
> @bar  
> end  
> end
> 
> p Foo.instance\_variables # ["@bar"]
> 
> p Foo.flurble # 123
> 
> Or did you mean something else?

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 26, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/instance-variable-names-available-from-class/161455/5 "2009-02-26T21:30:33Z")

</div>

On Feb 26, 2009, at 2:59 PM, Chris Gardner wrote:

> What I mean is that I would like to ask a class for the names of  
> instance variables of its objects, which may have yet to be  
> instantiated.

The question doesn’t apply in Ruby since instance variables are  
dynamically created on a per object basis. Even if you use standard  
helpers such as attr\_accessor and company, those methods simply create  
instance methods that manipulate instance variables–they don’t  
actually create the instance variables.

Just to be clear, there is no language-supported way to introspect a  
Ruby class and determine what instance variables may or may not be  
supported by all instances of an object. You can’t even introspect to  
find out what methods were created by the attr\* family of methods and  
what methods were simply defined explicitly. Sure you could come up  
with heuristics such as:

If there is a method called x and another called x= then there is  
probably an instance variable named @x.

But that is just a guess, not something supported by the language  
semantics.

The idea that each instance of a class can have its own behavior and  
state that is independent of its class definition and that can evolve  
over time is something that must be understood before a programmer can  
fully grasp Ruby’s programming and object model.

Gary W.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 27, 2009, 2:55am UTC](https://www.ruby-forum.com/t/instance-variable-names-available-from-class/161455/6 "2009-02-27T02:55:14Z")

</div>

On Thu, Feb 26, 2009 at 2:59 PM, Chris Gardner  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> I was trying to ask class Foo for the names @bar and @baz. I’ve found  
> out that these are accessible only through Foo instances.
> 
> I’m a Ruby newbie and am just today learning about Ruby’s approach to  
> metaprogramming.

[http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-variable-is-it-anyway](http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-variable-is-it-anyway)

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
