From: Sean O'Halpin Date: 2005-11-11T09:18:27+09:00 Subject: Re: A question about class variable On 11/10/05, Sam Kong wrote: > Hello! > > I have a question about class variable. > > class C1 > @@a = "C1" > def f > @@a > end > end > > class C2 < C1 > @@a = "C2" > end > > puts C2.new.f #=> "C2" If you add the line: puts C1.new.f #=> "C2" you'll see that the @@a you set in C2 is actually the same @@a as defined in C1. Class variables in Ruby 1.8.2 are a more like 'class globals'. All subclasses share the same variable. > I assume that the method f should behave polymorphically and the body > of f should be interpreted in the context of C2's instance method if I > call it via an instance of C2. Ah assumptions! ;) The body of f ~is~ interpreted in the context of C2's instance, but the @@a is scoped to the class in which f is ~defined~. HTH Regards, Sean