Class variables problem

I have used

class Test
def Test.Calc(x)
return x * 2
end
end

class Fred < Test
end

puts Fred.Calc(12)

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.

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

En réponse à Peter Hickman peter@semantico.com:

I have used

class Test
def Test.Calc(x)
return x * 2
end
end

class Fred < Test
end

puts Fred.Calc(12)

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.

ts wrote:

“P” == Peter Hickman peter@semantico.com writes:

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

class Fred < Test
        @rate = 10
end

puts Fred.Calc(12)
^D
120
pigeon%

Guy Decoux

ts wrote:

pigeon% ruby
class Test
def Test.Calc(x)
return @rate * x
end
end

class Fred < Test
@rate = 10
end

puts Fred.Calc(12)
^D
120
pigeon%

Guy Decoux

It never occured to me that instance variables would be usable without
an instance. Well you learn something new every day.

Thanks.

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)
^D
NoMethodError: undefined method ‘Calc’ for Fred:Class from (irb):9

Fred should inherit all the Test class method, isn’t it? Therefore it has
access to its variables. Thanks

It never occured to me that instance variables would be usable without
an instance.

Well, you have an instance this is the class

pigeon% ruby -e 'class A; @a = 12; end; p A.instance_variables'
["@a"]
pigeon%

Guy Decoux

Hi –

···

On Wed, 4 Dec 2002, Peter Hickman wrote:

ts wrote:

pigeon% ruby
class Test
def Test.Calc(x)
return @rate * x
end
end

class Fred < Test
@rate = 10
end

puts Fred.Calc(12)
^D
120
pigeon%

Guy Decoux

It never occured to me that instance variables would be usable without
an instance.

They’re not :slight_smile: You’ve got two instances here (the Class objects Test
and Fred).

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

class Test
    def Calc(x)

  You have defined an instance method

puts Fred.Calc(12)

  but you try to call a class method

NoMethodError: undefined method 'Calc' for Fred:Class from (irb):9

Guy Decoux

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.

Which is better in term of OO Best practices?

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

···

Peter Hickman peter@semantico.com wrote:

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.

Martin DeMello wrote:

···

Peter Hickman peter@semantico.com wrote:

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.

Thank you all for your help.

I understand little java, but I think this is the same of
public static variable in java or am I wrong ?

(Maybe this is a thing for the "how do I do this in ruby " wiki
page?)

···

On Wed, 4 Dec 2002 23:26:20 +0900, Peter Hickman peter@semantico.com wrote:

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.

Thank you all for your help.