# Understanding instance variables

**URL:** <https://rubytalk.org/t/understanding-instance-variables/17131>\
**Category:** ruby-talk\
**Created:** [23 March 2005 18:19 UTC](https://rubytalk.org/t/understanding-instance-variables/17131 "2005-03-23T18:19:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [23 March 2005 18:19 UTC](https://rubytalk.org/t/understanding-instance-variables/17131/1 "2005-03-23T18:19:58Z")

</div>

Hi,

what happens here?

&nbsp;&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;c = C.new

&nbsp;&nbsp;&nbsp;&nbsp;class \<\<c ; @x = 'X' ; end

&nbsp;&nbsp;&nbsp;&nbsp;puts C.instance\_variables, C.instance\_eval( '@x')  
&nbsp;&nbsp;&nbsp;&nbsp;puts c.instance\_variables, c.instance\_eval( '@x')  
&nbsp;&nbsp;&nbsp;&nbsp;puts @x

Output:

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

Thanks in advance

Bertram

> **···**
>
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [23 March 2005 18:30 UTC](https://rubytalk.org/t/understanding-instance-variables/17131/2 "2005-03-23T18:30:28Z")

</div>

> &nbsp;&nbsp;&nbsp;&nbsp;class \<\<c ; @x = 'X' ; end

You are working with the singleton class (see what is the value of self  
when you make the assignement to @x), use the singleton class

> &nbsp;&nbsp;&nbsp;&nbsp;puts c.instance\_variables, c.instance\_eval( '@x')  
> &nbsp;&nbsp;&nbsp;&nbsp;puts @x

&nbsp;&nbsp;&nbsp;puts class \<\<c; self end.instance\_variables,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class \<\<c; self end.instance\_eval( '@x')

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [25 March 2005 00:18 UTC](https://rubytalk.org/t/understanding-instance-variables/17131/3 "2005-03-25T00:18:52Z")

</div>

Hi,

> \> class \<\<c ; @x = 'X' ; end
> 
> You are working with the singleton class (see what is the value of self  
> when you make the assignement to @x), use the singleton class
> 
> \> puts c.instance\_variables, c.instance\_eval( '@x')  
> \> puts @x
> 
> &nbsp;&nbsp;&nbsp;puts class \<\<c; self end.instance\_variables,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class \<\<c; self end.instance\_eval( '@x')

That means all three of

&nbsp;&nbsp;&nbsp;&nbsp;c's Singleton \< C \< Object

can have their own instance variables.

> > c=class C ; self ; end.new  
> > s=class \<\< C ; self ; end  
> > [c,s,C,Object].zip( %w-I S C O-).each { |x,y| x.instance\_eval '@x='+y.inspect }  
> > [c,s,C,Object].collect { |x| x.instance\_eval '@x' }

=\> ["I", "S", "C", "O"]

Or is there something I missed?

Bertram

> **···**
>
> Am Donnerstag, 24. Mär 2005, 03:30:28 +0900 schrieb ts:
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<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:** [25 March 2005 00:30 UTC](https://rubytalk.org/t/understanding-instance-variables/17131/4 "2005-03-25T00:30:01Z")

</div>

Hi --

> Hi,
> 
> > \> class \<\<c ; @x = 'X' ; end
> > 
> > You are working with the singleton class (see what is the value of self  
> > when you make the assignement to @x), use the singleton class
> > 
> > \> puts c.instance\_variables, c.instance\_eval( '@x')  
> > \> puts @x
> > 
> > &nbsp;&nbsp;&nbsp;puts class \<\<c; self end.instance\_variables,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class \<\<c; self end.instance\_eval( '@x')
> 
> That means all three of
> 
> &nbsp;&nbsp;&nbsp;c's Singleton \< C \< Object
> 
> can have their own instance variables.
> 
> > > c=class C ; self ; end.new  
> > > s=class \<\< C ; self ; end

Did you mean class \<\< c ? (You had mentioned c's singleton class.)  
But either way...

> > > [c,s,C,Object].zip( %w-I S C O-).each { |x,y| x.instance\_eval '@x='+y.inspect }  
> > > [c,s,C,Object].collect { |x| x.instance\_eval '@x' }
> 
> =\> ["I", "S", "C", "O"]
> 
> Or is there something I missed?

... that's correct: any object can have its own instance variables.

David

> **···**
>
> On Fri, 25 Mar 2005, Bertram Scharpf wrote:
> 
> > Am Donnerstag, 24. Mär 2005, 03:30:28 +0900 schrieb ts:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [25 March 2005 10:49 UTC](https://rubytalk.org/t/understanding-instance-variables/17131/5 "2005-03-25T10:49:27Z")

</div>

Hi,

> \>  
> \> c's Singleton \< C \< Object  
> \>  
> \>can have their own instance variables.  
> \>  
> \>\>\>c=class C ; self ; end.new  
> \>\>\>s=class \<\< C ; self ; end
> 
> Did you mean class \<\< c ? (You had mentioned c's singleton class.)  
> But either way...

Eh, what's the difference?

> \>\>\>[c,s,C,Object].zip( %w-I S C O-).each { |x,y| x.instance\_eval '@x='+y.inspect }  
> \>\>\>[c,s,C,Object].collect { |x| x.instance\_eval '@x' }  
> \>=\> ["I", "S", "C", "O"]  
> \>  
> \>Or is there something I missed?
> 
> ... that's correct: any object can have its own instance variables.

Ah, thanks!

Bertram

> **···**
>
> Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:
> 
> > On Fri, 25 Mar 2005, Bertram Scharpf wrote:
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [25 March 2005 10:55 UTC](https://rubytalk.org/t/understanding-instance-variables/17131/6 "2005-03-25T10:55:42Z")

</div>

svg% ruby -e 'class A; end; p class \<\< A; self end; p class \<\< A.new; self end'  
#\<Class:A\>  
#\<Class:#\<A:0x40099f34\>\>  
svg%

Guy Decoux

> **···**
>
> > Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:
> > 
> > > Did you mean class \<\< c ? (You had mentioned c's singleton class.)  
> > > But either way...
> 
> > Eh, what's the difference?

---

<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:** [25 March 2005 11:42 UTC](https://rubytalk.org/t/understanding-instance-variables/17131/7 "2005-03-25T11:42:18Z")

</div>

Hi --

> **···**
>
> On Fri, 25 Mar 2005, Bertram Scharpf wrote:
> 
> > Hi,
> > 
> > Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:
> > 
> > > On Fri, 25 Mar 2005, Bertram Scharpf wrote:
> > > 
> > > > &nbsp;&nbsp;c's Singleton \< C \< Object
> > > > 
> > > > can have their own instance variables.
> > > > 
> > > > > > c=class C ; self ; end.new  
> > > > > > s=class \<\< C ; self ; end
> > > 
> > > Did you mean class \<\< c ? (You had mentioned c's singleton class.)  
> > > But either way...
> > 
> > Eh, what's the difference?
> 
> As examples of the "every object can have i.variables" principle, no  
> difference. But c's singleton and C's singleton are completely  
> different objects.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [25 March 2005 11:51 UTC](https://rubytalk.org/t/understanding-instance-variables/17131/8 "2005-03-25T11:51:50Z")

</div>

Hi,

> **···**
>
> Am Freitag, 25. Mär 2005, 19:55:42 +0900 schrieb ts:
> 
> > \> Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:  
> > \>\> Did you mean class \<\< c ? (You had mentioned c's singleton class.)  
> > \>\> But either way...
> > 
> > \> Eh, what's the difference?
> > 
> > svg% ruby -e 'class A; end; p class \<\< A; self end; p class \<\< A.new; self end'
> 
> Sure, the c in "class \<\<c" was meant to be lowercase, not  
> "class \<\<C". Sorry!
> 
> Thank you both.
> 
> Bertram
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)
