Hi --

On Fri, 11 Nov 2005, Sam Kong wrote:

> class C1
>  def f
>    @@a
>  end
> end
>
> class C2 < C1
>  @@a = "C2"
> end
>
> puts C2.new.f
> #Result: in `f': uninitialized class variable @@a in C1 (NameError)
>
>
> Why does it bother with C1's @@a if it returns C2's @@a?

Class variables are per-hierarchy, as you've seen, but if you create
one down the hierarchy, it does not get propagated upward.  So C1 has
no @@a, even though @@a was initialized in its subclass C2.

Matz described this in ruby-talk 19774 as "an error that Ruby does not
detect yet."  I was never sure if he meant a programmer error, or a
Ruby error :-)  Anyway, it's a bit of an anomaly.

In any case, class variables are likely to change greatly in Ruby 2.0,
becoming class/module-scoped rather than hierarchy scoped.


David

-- 
David A. Black
dblack / wobblini.net