Hello,
I want to write a class with a class variable that gets initialized as
soon as the class is defined. Something like this:
class BigFloat
@@precision = 40 # This doesn’t work.
…
end
The contents of @@precision affect the instantiation of the class, so
it has to be defined before I create any object.
I don’t want to replace @@precision by a constant (PRECISION) because I
want the user to be able to change its value. I’ll write accessor methods
so the user can type:
BigFloat.precision = 50
Does anyone know how I can accomplish this?
Thanks a lot.
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
(i defined this class in irb, but cleaned it up here to be easy to read)
class Thing
@@cvar = 1
def Thing.cvar
@@cvar
end
def Thing.cvar= new
@@cvar = new
end
def initialize new @ivar = new + Thing.cvar
end
def ivar @ivar
end
def ivar= new @ivar = new
end
end
the class variable gets set when the class is defined, then used to
initialize and object. and it can be changed via class methods, and the
new value is then used in later objects.
On Monday, February 17, 2003, at 03:33 , Daniel Carrera wrote:
Hello,
I want to write a class with a class variable that gets initialized as
soon as the class is defined. Something like this:
class BigFloat
@@precision = 40 # This doesn’t work.
…
end
The contents of @@precision affect the instantiation of the class, so
it has to be defined before I create any object.
I don’t want to replace @@precision by a constant (PRECISION) because I
want the user to be able to change its value. I’ll write accessor
methods
so the user can type: