# Getting object attributes

**URL:** <https://rubytalk.org/t/getting-object-attributes/13006>\
**Category:** ruby-talk\
**Created:** [12 August 2004 01:11 UTC](https://rubytalk.org/t/getting-object-attributes/13006 "2004-08-12T01:11:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@Joe\_Laughlin](https://rubytalk.org/u/Joe_Laughlin)\
**Post date:** [12 August 2004 01:11 UTC](https://rubytalk.org/t/getting-object-attributes/13006/1 "2004-08-12T01:11:11Z")

</div>

I want to do something similar to this:

class Foo  
&nbsp;&nbsp;@goo = 2  
&nbsp;&nbsp;@boo = 5  
end

foo = Foo.new

["goo", "boo"].each do |attribute|  
&nbsp;&nbsp;puts foo.attribute # should print out 2 on the first loop, 5 on the  
second loop  
end

Ideas?

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [12 August 2004 01:39 UTC](https://rubytalk.org/t/getting-object-attributes/13006/2 "2004-08-12T01:39:19Z")

</div>

Hi --

> I want to do something similar to this:
> 
> class Foo  
> &nbsp;&nbsp;@goo = 2  
> &nbsp;&nbsp;@boo = 5  
> end
> 
> foo = Foo.new
> 
> ["goo", "boo"].each do |attribute|  
> &nbsp;&nbsp;puts foo.attribute # should print out 2 on the first loop, 5 on the  
> second loop  
> end
> 
> Ideas?

Clarification requested:

Do you want instances of foo to be able to get access to the instance  
variables of Foo itself? Or do you want instances of Foo (such as  
foo) to have their own instances variables called @goo and @boo, and  
reader methods to access them? You could do a bunch of different  
variations on this but I'm not sure which scenario you're trying to  
implement.

David

> **···**
>
> On Thu, 12 Aug 2004, Joe Laughlin wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Sam\_McCall](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Sam\_McCall](https://rubytalk.org/u/Sam_McCall)\
**Post date:** [12 August 2004 10:26 UTC](https://rubytalk.org/t/getting-object-attributes/13006/3 "2004-08-12T10:26:16Z")

</div>

Joe Laughlin wrote:

> I want to do something similar to this:
> 
> class Foo  
> &nbsp;&nbsp;@goo = 2  
> &nbsp;&nbsp;@boo = 5  
> end
> 
> foo = Foo.new
> 
> ["goo", "boo"].each do |attribute|  
> &nbsp;&nbsp;puts foo.attribute # should print out 2 on the first loop, 5 on the  
> second loop  
> end

["@goo","@boo"].each do |attribute|  
&nbsp;&nbsp;&nbsp;puts foo.instance\_variable\_get attribute  
end

Also:  
foo.instance\_variables.each do |attribute|  
&nbsp;&nbsp;&nbsp;puts foo.instance\_variable\_get attribute  
&nbsp;&nbsp;&nbsp;foo.instance\_variable\_set nil  
end

Hope this helps.  
Sam

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [12 August 2004 11:20 UTC](https://rubytalk.org/t/getting-object-attributes/13006/4 "2004-08-12T11:20:32Z")

</div>

Hi --

> Joe Laughlin wrote:
> 
> \> I want to do something similar to this:  
> \>  
> \> class Foo  
> \> @goo = 2  
> \> @boo = 5  
> \> end  
> \>  
> \> foo = Foo.new  
> \>  
> \> ["goo", "boo"].each do |attribute|  
> \> puts foo.attribute # should print out 2 on the first loop, 5 on the  
> \> second loop  
> \> end  
> ["@goo","@boo"].each do |attribute|  
> &nbsp;&nbsp;&nbsp;puts foo.instance\_variable\_get attribute  
> end

That will print:

&nbsp;&nbsp;nil  
&nbsp;&nbsp;nil

@goo and @boo are instance variables of the Class object Foo, not of  
its instances. You'd have to do:

&nbsp;&nbsp;puts foo.class.instance\_variable\_get(attribute)

or, if you want to be direct:

&nbsp;&nbsp;puts Foo.instance\_variable\_get(attribute)

This is one of those times where a Class object, despite being a  
special thing in various ways, is behaving just like any old object.  
In fact, it's better to respect that, and not access its instance  
variables from outside unless it lets you -- which you could do  
with:

&nbsp;&nbsp;class Foo  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :goo, :boo  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;@goo = 2  
&nbsp;&nbsp;&nbsp;&nbsp;@boo = 5  
&nbsp;&nbsp;end

&nbsp;&nbsp;puts Foo.goo # 2

> Also:  
> foo.instance\_variables.each do |attribute|  
> &nbsp;&nbsp;&nbsp;puts foo.instance\_variable\_get attribute  
> &nbsp;&nbsp;&nbsp;foo.instance\_variable\_set nil

(You need two arguments for instance\_variable\_set 🙂

David

> **···**
>
> On Thu, 12 Aug 2004, Sam McCall wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Joe\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@Joe\_Laughlin](https://rubytalk.org/u/Joe_Laughlin)\
**Post date:** [12 August 2004 20:51 UTC](https://rubytalk.org/t/getting-object-attributes/13006/5 "2004-08-12T20:51:09Z")

</div>

David A. Black wrote:

> Hi --
> 
> > I want to do something similar to this:
> > 
> > class Foo  
> > &nbsp;&nbsp;@goo = 2  
> > &nbsp;&nbsp;@boo = 5  
> > end
> > 
> > foo = Foo.new
> > 
> > ["goo", "boo"].each do |attribute|  
> > &nbsp;&nbsp;puts foo.attribute # should print out 2 on the first  
> > loop, 5 on the second loop  
> > end
> > 
> > Ideas?
> 
> Clarification requested:
> 
> Do you want instances of foo to be able to get access to  
> the instance variables of Foo itself? Or do you want  
> instances of Foo (such as foo) to have their own  
> instances variables called @goo and @boo, and reader  
> methods to access them? You could do a bunch of  
> different variations on this but I'm not sure which  
> scenario you're trying to implement.
> 
> David

The second one, I believe.

> **···**
>
> > On Thu, 12 Aug 2004, Joe Laughlin wrote:

---

<div class="post-metadata">

**Author:** ![Sam\_McCall](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Sam\_McCall](https://rubytalk.org/u/Sam_McCall)\
**Post date:** [12 August 2004 14:47 UTC](https://rubytalk.org/t/getting-object-attributes/13006/6 "2004-08-12T14:47:42Z")

</div>

David A. Black wrote:

> That will print:
> 
> &nbsp;&nbsp;nil
> 
> @goo and @boo are instance variables of the Class object Foo, not of  
> its instances.

Rule #1 of this newsgroup should be: "Read the question twice" and  
Rule #2 should be "test your answer".  
The rules should be automatically appended to every post 🙂  
I looked at that and assumed those definitions were in the initialize method. To be fair, I'm fairly sure that \_was\_ the intention 😉

> > &nbsp;&nbsp;foo.instance\_variable\_set nil
> 
> (You need two arguments for instance\_variable\_set 🙂

comp.lang.ruby.instance\_variable\_set "@rule2" "test your answer"  
Better?

Thanks for that, I think it means it's time for me to sleep.  
Sam

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [12 August 2004 20:57 UTC](https://rubytalk.org/t/getting-object-attributes/13006/7 "2004-08-12T20:57:45Z")

</div>

Hi --

> David A. Black wrote:  
> \> Hi --  
> \>  
> \>  
> \>\> I want to do something similar to this:  
> \>\>  
> \>\> class Foo  
> \>\> @goo = 2  
> \>\> @boo = 5  
> \>\> end  
> \>\>  
> \>\> foo = Foo.new  
> \>\>  
> \>\> ["goo", "boo"].each do |attribute|  
> \>\> puts foo.attribute # should print out 2 on the first  
> \>\> loop, 5 on the second loop  
> \>\> end  
> \>\>  
> \>\>  
> \>\> Ideas?  
> \>  
> \> Clarification requested:  
> \>  
> \> Do you want instances of foo to be able to get access to  
> \> the instance variables of Foo itself? Or do you want  
> \> instances of Foo (such as foo) to have their own  
> \> instances variables called @goo and @boo, and reader  
> \> methods to access them? You could do a bunch of  
> \> different variations on this but I'm not sure which  
> \> scenario you're trying to implement.  
> \>  
> \>  
> \> David
> 
> The second one, I believe.

Then what you want would probably be something like:

&nbsp;&nbsp;class Foo  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :goo, :boo  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@goo, @boo = 2, 5  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;foo = Foo.new  
&nbsp;&nbsp;[:goo, :boo].each {|m| puts foo.send(m) }

David

> **···**
>
> On Fri, 13 Aug 2004, Joe Laughlin wrote:
> 
> > \> On Thu, 12 Aug 2004, Joe Laughlin wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Mark10](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@Mark10](https://rubytalk.org/u/Mark10)\
**Post date:** [12 August 2004 21:00 UTC](https://rubytalk.org/t/getting-object-attributes/13006/8 "2004-08-12T21:00:19Z")

</div>

Joe Laughlin wrote:

> David A. Black wrote:
> 
> > Hi --
> > 
> > > I want to do something similar to this:
> > > 
> > > class Foo  
> > > @goo = 2  
> > > @boo = 5  
> > > end
> > > 
> > > foo = Foo.new
> > > 
> > > ["goo", "boo"].each do |attribute|  
> > > puts foo.attribute # should print out 2 on the first  
> > > loop, 5 on the second loop  
> > > end
> > > 
> > > Ideas?  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > Clarification requested:
> > 
> > Do you want instances of foo to be able to get access to  
> > the instance variables of Foo itself? Or do you want  
> > instances of Foo (such as foo) to have their own  
> > instances variables called @goo and @boo, and reader  
> > methods to access them? You could do a bunch of  
> > different variations on this but I'm not sure which  
> > scenario you're trying to implement.
> > 
> > David  
> > &nbsp;&nbsp;&nbsp;  
> > The second one, I believe.

then this will do what you want

class Foo  
&nbsp;&nbsp;attr\_reader :goo, :boo  
&nbsp;&nbsp;def initialize()  
&nbsp;&nbsp;&nbsp;&nbsp;@goo = 2  
&nbsp;&nbsp;&nbsp;&nbsp;@boo = 5  
&nbsp;&nbsp;end  
end

foo = Foo.new

["goo", "boo"].each do |attribute|  
&nbsp;&nbsp;puts foo.send(attribute) # should print out 2 on the first  
loop, 5 on the second loop  
end

@goo and @boo need to be initialised within a method or else they're instance variables of Foos metaclass rather than Foo itself.

> **···**
>
> > > On Thu, 12 Aug 2004, Joe Laughlin wrote:
> 
> --  
> Mark Sparshatt

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [12 August 2004 14:53 UTC](https://rubytalk.org/t/getting-object-attributes/13006/9 "2004-08-12T14:53:51Z")

</div>

Hi --

> David A. Black wrote:  
> \> That will print:  
> \>  
> \> nil  
> \> nil  
> \>  
> \> @goo and @boo are instance variables of the Class object Foo, not of  
> \> its instances.  
> Rule #1 of this newsgroup should be: "Read the question twice" and  
> Rule #2 should be "test your answer".  
> The rules should be automatically appended to every post 🙂  
> I looked at that and assumed those definitions were in the initialize  
> method. To be fair, I'm fairly sure that \_was\_ the intention 😉

I think it may have been... still waiting to hear from the OP on my  
request for clarification 🙂

> \>\> foo.instance\_variable\_set nil  
> \> (You need two arguments for instance\_variable\_set 🙂  
> comp.lang.ruby.instance\_variable\_set "@rule2" "test your answer"  
> Better?

Yes -- you're only one comma away from having it parse 🙂

David

> **···**
>
> On Thu, 12 Aug 2004, Sam McCall wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net
