which return 24 and is what I expected. But what I wanted to do was
class Test
def Test.Calc(x)
return @@rate * x
end
end
class Fred < Test
@@rate = 10
end
puts Fred.Calc(12)
which
s2:3:in `Calc’: uninitialized class variable @@rate in Test (NameError)
from s2:11
Now the class method has been inherited (and named accordingly) and I
was expecting it to use the class variable as defined in Fred but it was
trying to pick up the class variable in Test. How can I have the
inheritence of a class method that accesses the class variable of the
child class?
This is a gross simplification, the class I am designing would really
benefit from this working as I hoped. Oh yes. I am using ruby 1.6.7
(2002-03-19) [i386-linux]
which return 24 and is what I expected. But what I wanted to do was
class Test
def Test.Calc(x)
return @@rate * x
end
end
class Fred < Test
@@rate = 10
end
puts Fred.Calc(12)
which
s2:3:in `Calc’: uninitialized class variable @@rate in Test
(NameError)
from s2:11
Now the class method has been inherited (and named accordingly) and I
was expecting it to use the class variable as defined in Fred but it was
trying to pick up the class variable in Test. How can I have the
inheritence of a class method that accesses the class variable of the
child class?
This is a gross simplification, the class I am designing would really
benefit from this working as I hoped. Oh yes. I am using ruby 1.6.7
(2002-03-19) [i386-linux]
Any pointers greatly appreciated.
I’m not sure, but you should put your rate variable in class test and your calc
method in class fred.
This way may work better.
Because I think it’s impossible to access a child member.
Or Unless you define it static.
In other programming language you could write:
fred::rate to access it from anywhere…
So, you have 2 solutions.
Sory, I can’t give you a ruby implementation because I’m starting to use it.
trying to pick up the class variable in Test. How can I have the
inheritence of a class method that accesses the class variable of the
child class?
Why you don’t use a class instance variable ?
Guy Decoux
There is actually no real reason to instantiate this class. I know that
that would solve the problem but truthfully it doesn’t make sense. Think
of it as a unit conversion class, each instance of the class would have
no actual instance specific differences.
For example we don’t make an instance of the Kernel or Maths classes do
we (or am I thinking Java again?)
There is actually no real reason to instantiate this class. I know that
that would solve the problem but truthfully it doesn't make sense. Think
of it as a unit conversion class, each instance of the class would have
no actual instance specific differences.
pigeon% ruby
class Test
def Test.Calc(x)
return @rate * x
end
end
this has been called like a class method, but Calc is an instance method.
additionally Calc should not be capitalized, else it is also a constant.
finally, @rate is not set in an instance method, so it is not an instance
variable (think about what self is outside of any instance methode, but inside
a class definition - self == Class)
try
class Test
def calc(x)
return @rate * x
end
end
class Fred < Test
def initialize @rate = 10
end
end
fred = Fred.new
puts fred.calc 12 >> 120
-a
···
On Wed, 4 Dec 2002, Useko Netsumi wrote:
Why would the following did not work?
class Test
def Calc(x)
return @rate*x
end
end
class Fred < Test @rate = 10
end
puts Fred.Calc(12)
–
====================================
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
Now I used to think that instance variables only existed when you
created an object by the new method, x = Fred.new, and that you would
have to access it from x.
It’s not a problem it’s just not quite how I thought it was going to work.
Well, there are object instance variables, which are created when you
create an object by the new method. And then there are class instance
variables, which are instance variables of the class object. When you
say ‘class MyClass’, you are in effect saying ‘MyClass = Class.new’,
i.e. instantiating an object of class Class.
Now I used to think that instance variables only existed when you
created an object by the new method, x = Fred.new, and that you would
have to access it from x.
It’s not a problem it’s just not quite how I thought it was going to work.
Now I used to think that instance variables only existed when you
created an object by the new method, x = Fred.new, and that you would
have to access it from x.
It’s not a problem it’s just not quite how I thought it was going to work.
Well, there are object instance variables, which are created when you
create an object by the new method. And then there are class instance
variables, which are instance variables of the class object. When you
say ‘class MyClass’, you are in effect saying ‘MyClass = Class.new’,
i.e. instantiating an object of class Class.
martin
Ah ha. That clears it up for me. I was slipping into a Java moment there
were a class doesn’t exists until you create it (reflection aside). Well
I think I understand now.
Ah ha. That clears it up for me. I was slipping into a Java moment there
were a class doesn’t exists until you create it (reflection aside). Well
I think I understand now.