Trans
8/5/2006 7:23:00 PM
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.