[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Inheritance with class-variables

Yochen Gutmann

8/5/2006 6:52:00 PM

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

--
Posted via http://www.ruby-....

5 Answers

Trans

8/5/2006 7:23:00 PM

0


Yochen Gutmann wrote:
> 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.


Marcin Mielzynski

8/5/2006 7:31:00 PM

0

Yochen Gutmann 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

Yochen Gutmann

8/5/2006 10:58:00 PM

0

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

--
Posted via http://www.ruby-....

Rick DeNatale

8/6/2006 5:01:00 AM

0

On 8/5/06, Trans <transfire@gmail.com> 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.


--
Rick DeNatale

Yochen Gutmann

8/6/2006 2:24:00 PM

0

Hey folks,

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

Greetings,
Yochen

--
Posted via http://www.ruby-....