initializing class variables 
Author Message
 initializing class variables

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

    ...
end


it has to be defined before I create any object.


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



Sat, 06 Aug 2005 04:33:22 GMT  
 initializing class variables
Quote:
----- Original Message -----

class BigFloat

    ...
end
----------------------------

Why do you say it doesn't work?  (In other words, how is it not working for
you?)

  irb(main):001:0> class Foo

  irb(main):003:1>
  irb(main):004:1*   def Foo.bar

  irb(main):006:2>   end
  irb(main):007:1> end
  nil
  irb(main):008:0> Foo.bar
  5
  irb(main):009:0>

Chris



Sat, 06 Aug 2005 04:52:39 GMT  
 initializing class variables

Quote:

> Why do you say it doesn't work?  (In other words, how is it not working for
> you?)

Ooops.  Actually the problem was on the wrapping function that I wrote.  
It was sending a blank value for 'precision'.

Thanks for the help.

--
Daniel Carrera
Graduate Teaching Assistant.  Math Dept.
University of Maryland.  (301) 405-5137



Sat, 06 Aug 2005 04:59:03 GMT  
 initializing class variables

Quote:

> 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

>     ...
> end


> it has to be defined before I create any object.


> 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.

it works in 1.6.7

(i defined this class in irb, but cleaned it up here to be easy to read)

class Thing

   def Thing.cvar

   end
   def Thing.cvar= new

   end
   def initialize new

   end
   def ivar

   end
   def ivar= new

   end
end

irb(main):019:0> Thing.cvar
1
irb(main):020:0> t = Thing.new 1

irb(main):021:0> Thing.cvar = 10
10
irb(main):022:0> Thing.cvar
10
irb(main):023:0> t = Thing.new 1

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.

is this what you wanted?

-Justin White


http://tin.2y.net/
AIM: just6979



Sat, 06 Aug 2005 05:11:15 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Initializing class variables with a file

2. What is best way to initialize class variables

3. Why can't I INITIALIZE a class variable with another class var

4. KeyEvent's CtrlKeyMask class variable is not initialized

5. How to initialize class-wide objects (variables)?

6. Class variables / global variables / Init variables

7. newbie question: class method initialize

8. Initializing classes

9. Initializing classes on startup

10. how to initialize expanded class

11. Extend initialize method in String Class

12. Creating an instance of a class without calling initialize()

 

 
Powered by phpBB® Forum Software