Inheritance with class-variables

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 “[email protected]@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 :wink:

Thanx,
Yochen

Yochen G. wrote:

puts "My body temperature is "[email protected]@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 :wink:

I think this behavior has changed for 1.9.

T.

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

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

On 8/5/06, Yochen G. [email protected] 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/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/

On 8/5/06, Trans [email protected] 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.

Hey folks,

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

Greetings,
Yochen

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs