From: Joel VanderWerf Date: 2002-02-12T04:19:06+09:00 Subject: Re: Class variable madness Yukihiro Matsumoto wrote: ... > That's what class variables are. Class variables are variables to be > shared among class hierarchy. They are not only shared by instance of > the class, but also shared by instances of subclasses. As a result, > you cannot define class variables of same names in subclass. > > matz. ...unless you define it in the subclass *first*, right? irb(main):001:0> class A; end nil irb(main):002:0> class B class A; @@x=1; end 1 irb(main):004:0> class B; @@x; end 0 irb(main):005:0> class A; @@x; end 1 So the semantics depends on the order of definition. Because of this behavior, I treat class vars sort of like globals and use them only in prototype code, quick and dirty code, etc. For code that I can think about more (and expend more keystrokes), I use class instance variables, and write a bunch of accessors. -- Joel VanderWerf California PATH, UC Berkeley mailto:vjoel@path.berkeley.edu Ph. (510) 231-9446 http://www.path.berkeley.edu FAX (510) 231-9512