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.
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.
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.
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.
"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.
···
On 8/5/06, Yochen Gutmann <yoche2001@dergutemann.com> wrote:
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.