Hello --

On Sat, 11 Aug 2001, Tobias Reif wrote:

> Hi
>
> It seems as if subclasses just extend their parentclasses, but are no
> classes themselves. So class variables get overridden by the last
> assignment. This is counterintuitive.

That's the thing, though: subclasses *are* classes themselves.  If
they weren't, Object would be the only real class :-)

> David Black's example shows this, and also creates a problem to be
> solved, because it has semantic logic:
> (generalizations)
> A car is a vehicle with 4 tires.
> A Bike is a vehikle with 2 tires.
> How to implement these circumstances nicely in Ruby, when all subclasses
> share class variables with their parents, and override them
> 'arbitrarily'? Using def initialize? I'm sure a nice solution is
> possible, but the behaviour David Black describes is still disturbing.

You can definitely do something like this without class variables; for
example:

  class Vehicle
    def tires
    end

    def talk
      puts "I have #{tires} tires"
    end
  end

  class Car < Vehicle
    def tires ; 4 ; end
  end

  class Bike < Vehicle
    def tires ; 2 ; end
  end

  Car.new.talk      # => I have 4 tires


or using constants:

  class Vehicle
    TIRES = nil
    def talk
      puts "I have #{type::TIRES} tires"
    end
  end

  class Car < Vehicle
    TIRES = 4
  end

  class Bike < Vehicle
    TIRES = 2
  end

  Car.new.talk


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav