Inheritance with class-variables

Y

Yochen Gutmann

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
 
T

Trans

Yochen said:
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 ;-)

I think this behavior has changed for 1.9.

T.
 
M

Marcin Mielżyński

Yochen said:

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
 
Y

Yochen Gutmann

Hi Lopex,
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
 
R

Rick DeNatale

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.
 
Y

Yochen Gutmann

Hey folks,

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

Greetings,
Yochen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top