"Eustaquio Rangel de Oliveira Jr." <eustaquiorangel / yahoo.com> schrieb im
Newsbeitrag news:41D4003E.2060503 / yahoo.com...
> Hello there.
>
> I was talking with chris2 and matju on the irc channel and they gave me
> good explanation about this point, but I'd like to share it with you and
> discuss something about it.
>
> I was asking about if, when defined on a parent class, a class variable
> overwrites the class variables with the same name on it's children.

The short answer is the one that David gave implicite: just don't use
them.  IMHO you can safely ignore this language feature without loosing
much but gaining a lot (understandability of code etc.).

As Guy pointed out, you should really be counting the number of cars in
the individual car classes.  That's what he's doing with his instance
variable accesses.

I would probably model your example completely different, because Honda
and Ford are really instances of CarBuilder not really sub classes.  So
this is the result and suddenly we don't need class vars any more:

class CarBuilder
  attr_reader :name, :total_of_cars

  def initialize(name)
    @name = name
    @total_of_cars = 0
  end

  def build
    puts "#{name} builds another car"
    @total_of_cars += 1
  end
end

h = CarBuilder.new "Honda"
f = CarBuilder.new "Ford"

h.total_of_cars
f.total_of_cars

h.build
h.build
h.build
f.build

h.total_of_cars
f.total_of_cars


?> h = CarBuilder.new "Honda"
=> #<CarBuilder:0x101865d0 @name="Honda", @total_of_cars=0>
>> f = CarBuilder.new "Ford"
=> #<CarBuilder:0x10183af8 @name="Ford", @total_of_cars=0>
>>
?> h.total_of_cars
=> 0
>> f.total_of_cars
=> 0
>>
?> h.build
Honda builds another car
=> 1
>> h.build
Honda builds another car
=> 2
>> h.build
Honda builds another car
=> 3
>> f.build
Ford builds another car
=> 1
>>
?> h.total_of_cars
=> 3
>> f.total_of_cars
=> 1

Regards

    robert