I create a Vehicle
super class and MyCar
,MyTruck
sub classes. There is @@number_of_vehicles
class variables that keeps track of number of Instances instanitiated from the Sub Classes.
- And a class method that prints that class variable
But it prints 0
even after creating objects from the sub classes.
class Vehicle
@@number_of_vehicles = 0
def self.number_of_vehicles
puts "This program has created #{@@number_of_vehicles} vehicles"
end
def self.gas_mileage(gallons, miles)
puts "#{miles / gallons} miles per gallon of gas"
end
def initialize
@@number_of_vehicles += 1
end
end
class MyCar < Vehicle
NUMBER_OF_DOORS = 4
#code omitted for brevity...
end
class MyTruck < Vehicle
NUMBER_OF_DOORS = 2
end