# Question about class variables and instance variables

**URL:** <https://www.ruby-forum.com/t/question-about-class-variables-and-instance-variables/53303>\
**Category:** Ruby\
**Created:** [January 31, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/question-about-class-variables-and-instance-variables/53303 "2006-01-31T20:29:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@ericd](https://www.ruby-forum.com/u/ericd)\
**Post date:** [January 31, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/question-about-class-variables-and-instance-variables/53303/1 "2006-01-31T20:29:31Z")

</div>

Hello,

I’ve some difficolties with instance variables and classe variables

With :

class A  
def foo  
@foo  
end  
def bar  
@@bar  
end  
end

```
a = A.new

```

* * *

| | | | | |  
| a |–(instance\_of)-\>-| A |–(instance\_of)-\>-| Class |  
| | | | | |

* * *

a is an instance of A which itself is an instance of Class  
@foo is an instance variable, so it is stored in box “a”  
@@bar is a class variable, so it is stored in box “A”

if I do something like the following :

class A  
def A.strange  
@strange  
end  
end

Where is stored my @strange ? I had the hypothesis that @strange in the  
“A” context was equivalent to @@strange in the “a” context but the  
following code prove me to be wrong :

class A  
def A.strange  
@strange  
end  
def strange=( val )  
@@strange = val  
end  
end

a = A.new  
a.strange= 2  
p A.strange # output is nil, not 2

Could someone explain me where to place this @strange ? in which box  
could I store it ?

## Thanks in advance

Eric D.

---

<div class="post-metadata">

**Author:** ![ericd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@ericd](https://www.ruby-forum.com/u/ericd)\
**Post date:** [January 31, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/question-about-class-variables-and-instance-variables/53303/2 "2006-01-31T21:13:42Z")

</div>

On Jan 31, 2006, at 2:29 PM, Eric D. wrote:

> ```
> def bar
> 
> ```
> 
> * * *
> 
> ```
> @strange
> @strange
> 
> ```
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Classes are objects too. @strange is an instance variable of the  
object A (which has class, Class). @@strange is a class variable of  
the class A. The difference is that instances of A can peek at the  
class vars. They can’t peek at A’s instance vars. They are in two  
separate namespaces. The other difference is that class variables  
aren’t so much class variables as they are class hiearchy vars. EG:

class A  
@@strange = 1  
end

class B \< A  
puts @@strange  
end

#prints 1

class C  
@strange = 1  
end

class D \< C  
puts @strange  
end

# prints nil

---

<div class="post-metadata">

**Author:** ![ericd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@ericd](https://www.ruby-forum.com/u/ericd)\
**Post date:** [January 31, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/question-about-class-variables-and-instance-variables/53303/3 "2006-01-31T21:07:39Z")

</div>

On Jan 31, 2006, at 11:29 AM, Eric D. wrote:

> ```
> end
> 
> ```
> 
> a is an instance of A which itself is an instance of Class  
> end  
> def strange=( val )  
> could I store it ?  
> instance variables are always stored in the instance self.

class variables are shared between a class, its instances, its  
subclasses and its subclass instances.

Consider:

class A  
@@a\_1 = :one  
def initialize  
@@a\_2 = :two  
end  
end

A.new

class B \< A  
p @@a\_1, @@a\_2

@@b\_1 = :three  
def initialize  
@@b\_2 = :four  
end

def print  
p @@a\_1, @@a\_2, @@b\_1, @@b\_2  
end  
end

class A  
x = @@b\_1 rescue ‘not found’  
y = @@b\_2 rescue ‘not found’

p x, y  
end

B.new.print

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![ericd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@ericd](https://www.ruby-forum.com/u/ericd)\
**Post date:** [February 2, 2006, 12:54am UTC](https://www.ruby-forum.com/t/question-about-class-variables-and-instance-variables/53303/4 "2006-02-02T00:54:03Z")

</div>

On 2006-01-31, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class variables are shared between a class, its instances, its  
> subclasses and its subclass instances.

Is this really the whole story? Because until now I always thought I  
didn’t understand class variables but if this is so then now I do.  
Wheeeee!

But there’s at least one gotcha; a class variable shadows any class  
variable of the same name that you subsequently create in an ancestor  
class:

```
$ irb
irb(main):001:0> class A ; end ; class B < A ; end
=> nil
irb(main):002:0> class B ; @@x = 1 ; end ; class A ; @@x = 0 ; end
=> 0
irb(main):003:0> class A ; puts @@x ; end ; class B ; puts @@x ; end
0
1
=> nil
irb(main):004:0> exit

```

… but if you create the class variable in the ancestor class first  
then the descendant class inherits it instead of creating a new one:

```
$ irb
irb(main):001:0> class A ; end ; class B < A ; end
=> nil
irb(main):002:0> class A ; @@x = 0 ; end ; class B ; @@x = 1 ; end
=> 1
irb(main):003:0> class A ; puts @@x ; end ; class B ; puts @@x ; end
1
1
=> nil
irb(main):004:0> exit

```

Any other subtleties?

Cheers,

Jeremy H.
