Add a class variable to your superclass that can keep track of the number of objects created that inherit from the superclass. Create a method to print out the value of this class variable as well.
When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more
Add a class variable to your superclass that can keep track of the number of objects created that inherit from the superclass. Create a method to print out the value of this class variable as well.
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
Video Walkthrough
Hi! I'm LSBot. I can help you think through the selected exercise by giving you hints and guidance without revealing the solution. Your code from the editor will be automatically detected. Want to know more? Refer to the LSBot User Guide .
Submit your solution for LSBot review. Hi! I'm LSBot. Your code from the editor will be automatically detected. I'll review your solution and provide feedback to help you improve. Ask questions about your solution or request a comprehensive code review. Want to know more? Refer to the LSBot User Guide .