How to add an instance of a class into an array of another class (Ruby)
I have two files in my directory, one which is garage.rb
& another called car.rb
.
car.rb
file just holds a car object:
class Car
end
garage.rb
file is as follows:
require 'car.rb' #Makes car class accessible in garage.rb
class Garage
def initialize
@capacity = []
end
attr_accessor :capacity
end
When I make a new instance of a car
by calling car = Car.new
, how do I put car
object in the @capacity
array my default?
Essentially, whenever I call car = Car.new
, I want car
to be put in the @capacity
array instantly.
I understand that I could make a function like so:
def add_car_to_garage(car)
capacity << car
end
But, I want the car to start in the garage when it is created, so I don't want a method to add it to the array, I just want it to automatically start there when the instance of car
is created.
Any advice would be appreciated. Thank you.
Comments
Post a Comment