From: German Monfort Date: 2007-05-01T02:52:58+09:00 Subject: Re: Class Level Variables > 2) Class variables are associated with a class *and* its subclasses > and the order of initialization is important. > > class C > @@foo = 42 # shared by C and its decendents! > def foo > @@foo # shared with C, D, and E > end > def bar > @@bar # this is C's @@bar > end > end > > class D < C > end > > class E < C > @@foo = 43 # @@foo shared with C, D, and E > @@bar = 44 # @@bar only defined for E > def bar > @@bar # this is E's @@bar > end > end > > puts D.new.foo # 43 > > puts E.new.bar # 44 > puts C.new.bar # undefined! > > class C > @@bar = 45 > end > > puts C.new.bar # 45 > puts E.new.bar # still 44 > > Gary Wright Shouldn't be puts D.new.foo � � � # 42 ? Because D < C and has no foo method defined (so it uses C' foo) and also has no @@foo defined (so it uses C's @@foo) which is set to 42 not 43. I'm confused here.