# Inheritance with class-variables

**URL:** <https://www.ruby-forum.com/t/inheritance-with-class-variables/101535>\
**Category:** Ruby\
**Created:** [August 5, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/inheritance-with-class-variables/101535 "2006-08-05T20:51:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [August 5, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/inheritance-with-class-variables/101535/1 "2006-08-05T20:51:41Z")

</div>

Hi,

I am quite new into ruby and stumbled over some strange (at least to me)  
behavior.  
Have a look and guess what the output of this code might be:

class Animal  
@@temperature = 0

def info\_temperature  
puts “My body temperature is “+@@temperature.to\_s+” degree”  
end  
end

class Spider \< Animal  
@@temperature = 20  
end

class Horse \< Animal  
@@temperature = 38  
end

batty = Spider.new  
fury = Horse.new

batty.info\_temperature  
fury.info\_temperature

Well, for myself, I’d expected that the result were “My body temperature  
is 20 degree” and “My body temperature is 38 degree”.

But surprise, surprise, both animals tell me that they have 38 degree!  
The next strange thing is that this effect seems to depend on the order  
of the class-definition.

How does this happen? And Why 😉

Thanx,  
Yochen

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [August 5, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/inheritance-with-class-variables/101535/2 "2006-08-05T21:24:30Z")

</div>

Yochen G. wrote:

> ```
> puts "My body temperature is "+@@temperature.to_s+" degree"
> 
> ```
> 
> The next strange thing is that this effect seems to depend on the order  
> of the class-definition.
> 
> How does this happen? And Why 😉

I think this behavior has changed for 1.9.

T.

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [August 5, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/inheritance-with-class-variables/101535/3 "2006-08-05T21:36:32Z")

</div>

Yochen G. wrote:

> Hi,

change it to:

class Animal  
@temperature = 0

def self.temperature  
@temperature  
end

def info\_temperature  
puts “My body temperature is “+self.class.temperature.to\_s+”  
degree”  
end  
end

class Spider \< Animal  
@temperature = 20  
end

class Horse \< Animal  
@temperature = 38  
end

batty = Spider.new  
fury = Horse.new

batty.info\_temperature  
fury.info\_temperature

the @@variables are not class object variables - they resemble static  
variables from in c++/java, it’s not the same.

lopex

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [August 6, 2006, 12:57am UTC](https://www.ruby-forum.com/t/inheritance-with-class-variables/101535/4 "2006-08-06T00:57:35Z")

</div>

Hi Lopex,

Marcin MielÅ¼yÅ?ski wrote:

> def info\_temperature  
> puts “My body temperature is “+self.class.temperature.to\_s+”  
> degree”  
> end

indeed, it worked! Thanx for your hint. But still I cant see how Ruby’s  
class variables work. Do you know any good literature about that topic?  
I poke around in the pickaxe but found nothing about this specific  
topic.

Greetings,  
Yochen

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [August 6, 2006, 6:31am UTC](https://www.ruby-forum.com/t/inheritance-with-class-variables/101535/5 "2006-08-06T06:31:22Z")

</div>

On 8/5/06, Yochen G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class variables work. Do you know any good literature about that topic?  
> I poke around in the pickaxe but found nothing about this specific  
> topic.

Pickaxe 2nd edition p 332

“Class variables are shared by children of the class in which they are  
first defined”

followed by an example.

The section called “Class Instance Variables” starting on page 388,  
describes the syntax and semantics of instance variables of classes.  
These are NOT the same as class variables, which are really just  
variables defined in the namespace of the class.

In fact, it might be more accurate to call those @@variables module  
variables instead, since they can be defined within modules. After all  
classes can’t do much more than modules can. Class inherits almost  
everything from Module, except for the allocation of instances, and  
the ability to form an inheritance hierarchy.

–  
Rick DeNatale

IPMS/USA Region 12 Coordinator  
[http://ipmsr12.denhaven2.com/](http://ipmsr12.denhaven2.com/)

Visit the Project Mercury Wiki Site  
[http://www.mercuryspacecraft.com/](http://www.mercuryspacecraft.com/)

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [August 6, 2006, 7:01am UTC](https://www.ruby-forum.com/t/inheritance-with-class-variables/101535/6 "2006-08-06T07:01:25Z")

</div>

On 8/5/06, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think this behavior has changed for 1.9.

Has the behavior (i.e. class variables are shared between classes and  
their subclasses) changed, or just the access control to the  
variables.

In 1.8 class variables can be accessed anywhere in the containing  
module or class. The pickaxe says that 1.9 will make them private to  
the class which contains them, but that still means that they are  
accessible to subclasses.

The distinction between class variables, and class instance variables  
is shared with, and perhaps inspired by Smalltalk by the way.

---

<div class="post-metadata">

**Author:** ![yoche2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yoche2001](https://www.ruby-forum.com/u/yoche2001)\
**Post date:** [August 6, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/inheritance-with-class-variables/101535/7 "2006-08-06T16:24:25Z")

</div>

Hey folks,

thanks a lot for your kind support. This all made my picture of Ruby’s  
OO-features much clearer.

Greetings,  
Yochen
