From: "David Alan Black" <dblack / candle.superlink.net>
> I've been puzzling over Ruby's treatment of class variables -- not so
> much the "what" as the "why".
>
> It seems strange to me that a class somewhere off in one side of an
> inheritance tree can affect a class variable all over the tree.
>
> For illustration purposes (no claims of greatness of design):
>
>    class Vehicle
>      @@tires = 0
>      def tire_count
>        @@tires
>      end
>    end
>
>    class Car < Vehicle
>      @@tires = 4
>    end
>
>    class Bike < Vehicle
>      @@tires = 2
>    end
>
>    puts Car.new.tire_count  # => 2
>
>
> Class variables seem to be not so much per class as per class
> hierarchy or family.  I can't quite figure out why they work this way,
> rather than working like instance methods (so that redefinition in a
> child class stops the search process up the inheritance path).
>
> Insights, opinions, and/or words of wisdom welcome.

This makes sense to me.  @@tyres is defined as a class variable of Vehicle.
Any class derived from Vehicle has access to its instance and class
variables, as per usual scoping rules.  Creating the Car and Bike classes
overwrite the value of the @@tyres variable in Vehicle, rather than create a
new variable named @@tyres, because @@tyres is brought into scope by the "<
Vehicle" declaration.

So, yes it's confusing that Car and Bike don't create new class variables.
But conversely it's not confusing that class variables follow the same
scoping rules as instance variables.  It's pretty even either way.

I consider this a result of Ruby not requiring variable declarations. E.g.
if a variable 'x' is defined in a scope, then it is defined in any enclosed
scopes, even though those enclosed scopes might want to create a new
variable of that name. There is no way for scopes to declare that they want
a *new* variable rather than use the existing variable.