From: "David A. Black" Date: 2009-11-30T06:24:11+09:00 Subject: Re: Class variables, instance variables, singleton; Ruby v. C++ Hi -- On Sat, 28 Nov 2009, Rick DeNatale wrote: > As others have pointed out class variables are a pretty wonky area of > Ruby. In Ruby 1.8 if G is a actually a subclass of F, then whether or > not @@x is the same instance variable for both depends in some cases > on whether or not F got its instance variable before G did or not. > > When a class variable is initialized Ruby 1.8 looks through the chain > of superclasses to see if a class variable with that name already > exists, and if so uses that definition. If not it defines it for the > current class. Ruby 1.9 changed that so that now class variables are > no longer shared with subclasses. I think that was true briefly but not more recently: $ irb191 irb(main):001:0> class C irb(main):002:1> @@x = 1 irb(main):003:1> end => 1 irb(main):004:0> class D < C irb(main):005:1> @@x = 2 irb(main):006:1> end => 2 irb(main):007:0> class C irb(main):008:1> @@x irb(main):009:1> end => 2 (unless it's been re-reverted yet more recently and I haven't picked up on it). David -- THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally! January 22-23, Tampa, Florida Info and registration at http://www.thecompleatrubyist.com -------------------------------------- My new job: http://tinyurl.com/yfpn9hz