# Instance variables as class variable?

**URL:** <https://www.ruby-forum.com/t/instance-variables-as-class-variable/120330>\
**Category:** Ruby\
**Created:** [November 15, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/instance-variables-as-class-variable/120330 "2007-11-15T21:15:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 15, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/instance-variables-as-class-variable/120330/1 "2007-11-15T21:15:26Z")

</div>

I’m trying to get a handle on why the following code works by printing  
out ‘frog’  
because @x looks like instance variable syntax. I know that some of  
these things don’t work the way I’d expect.

class Test  
@x = ‘frog’

class \<\< self  
attr\_reader :x  
end

end

puts Test.x

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 15, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/instance-variables-as-class-variable/120330/2 "2007-11-15T22:29:13Z")

</div>

On Nov 15, 2007 3:15 PM, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> attr\_reader :x  
> end
> 
> end
> 
> puts Test.x

Well it is an instance variable, of the class. This is commonly  
called a class instance variable, not to be confused with a class  
variable.

Within the lexical scope of class…end self is the class, so @x is an  
instance variable of the class.

Consider this:

class Test

```
# self here is Test which holds the methods for its instances

attr_reader :x # this is actually a message sent to Test, which

```

defines an instance method  
end

Now when you say  
class Test  
class \<\< self # (which could also be class \<\< Test)  
#self becomes the singleton class of Test.  
attr\_reader :x # and here the attr\_reader message is sent to  
Test’s singleton class  
# resulting in an “instance method” in  
the singleton class which is a  
# class method of Test

end  
end

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 16, 2007, 12:08am UTC](https://www.ruby-forum.com/t/instance-variables-as-class-variable/120330/3 "2007-11-16T00:08:43Z")

</div>

@x is an instance variable belonging to the object-that-is-a-class,  
not to an-object-that-is-an-instance-of-that-class. I cover this with  
some helpful pictures here:  
[http://www.visibleworkings.com/little-ruby/](http://www.visibleworkings.com/little-ruby/)

Drawing pictures really helps in figuring out what’s going on.

On Nov 15, 2007, at 2:15 PM, removed\_email\_address@domain.invalid wrote:

> attr\_reader :x  
> end
> 
> end
> 
> puts Test.x

* * *

Brian M., independent consultant  
Mostly on agile methods with a testing slant  
[www.exampler.com](http://www.exampler.com), [Exploration Through Example](http://www.exampler.com/blog), [twitter.com/marick](http://twitter.com/marick)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 16, 2007, 5:15am UTC](https://www.ruby-forum.com/t/instance-variables-as-class-variable/120330/4 "2007-11-16T05:15:34Z")

</div>

Hi,

Thanks for all the helpful comments, I will have a look at that site  
and study this some more. I kind of understand I think the gist of  
this. I do love Ruby and Rails to the point of almost an obsession,  
but my first impression of this particular instance question is that  
it seems sort of unintuitive at first pass anyway.

When I first read about class and instance variables I thought I  
understood it until I saw some of these other types of examples. I  
wrote another test program below which prints the 2 different values  
of @x, I was out walking the dog thinking about this and I decided I  
needed to try this one example out to make sure I knew what it did,  
and I wasn’t exactly sure what it was going to do until I tried it.

#=========================

class Test  
@x = ‘frog’

def initialize  
@x = “green”  
end

def prx  
puts @x  
end

class \<\< self  
attr\_accessor :x  
end

end

puts Test.x  
test = Test.new  
test.prx
