I’m bad when I try to explain with my words, so I’ll just post code:
#--------------------------------------------------------
class Vehicle
attr_accessor :test
@@array_of_instances = []
def self.array_of_instances
@@array_of_instances
end
def initialize(t)
@test = t
@@array_of_instances << self
end
end
class Car < Vehicle
end
vehicle1 = Vehicle.new(“truck”)
vehicle2 = Vehicle.new(“ship”)
car1 = Car.new(“Peugeot”)
puts "Vehicles: " + Vehicle.array_of_instances.inspect
puts
puts "Cars: " + Car.array_of_instances.inspect
#--------------------------------------------------------
Problem: “Vehicle.array_of_instances” and “Car.array_of_instances” have
same 3 instantiated objects !
Question: How do I make (this example) “Vehicle.array_of_instances” to
have only two objects with attributes “truck” and “ship” and
“Car.array_of_instances” to have only one object with attribute
“peugeot” ?
