From: David Alan Black Date: 2002-02-12T05:22:23+09:00 Subject: Re: Class variable madness Hello -- On Tue, 12 Feb 2002, Joel VanderWerf wrote: > 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 0 > irb(main):003:0> 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. This came up in some discussion about class variables last year. Matz said in [ruby-talk:19974]: I know this happens. I consider this as an error that Ruby does not detect yet. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav