From: "David A. Black" Date: 2005-11-27T09:36:42+09:00 Subject: Re: Nubish questions about syntax and gems Hi -- On Sun, 27 Nov 2005, Ross Bamford wrote: > On Sat, 26 Nov 2005 21:44:00 -0000, David A. Black > wrote: > >> What you've got basically is: >> >> class A >> @@foo = "A's class variable foo" >> end >> @@foo = "Object's class variable foo" >> class B >> @@foo = "B's class variable foo" >> end >> >> Since B is a subclass of Object, setting B's @@foo also sets Object's >> @@foo. The reason A's @@foo (when you see it via the instance eval) >> does not change is that if you create a subclass's class variable >> *first*, and then the superclass's (in this case, A and Object, >> respectively), they are separate: >> >> irb(main):002:0> class A; end; class B < A; @@foo = 1; end; class A; >> @@foo = 2; puts @@foo; end >> 2 >> => nil >> irb(main):003:0> class B; puts @@foo; end >> 1 >> David > > That's pretty subtle. Definitely one for the notebook. Keep in mind too that class variable behavior is due to change in 2.0 (and I guess 1.9 too). The new behavior is supposed to be more strictly class-scoped, rather than hierarchy scoped. David -- David A. Black dblack@wobblini.net