Search code examples
arraysrubyclassobjectinstance

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.


Solution

  • You can use the ObjectSpace module with each_object method inside your Car class to define method to keep track for each living instances, like this:

    class Car
    
      def self.all
        ObjectSpace.each_object(self).to_a
      end
    end
    
    require_relative 'car' # Makes car class accessible in garage.rb 
                                                                     
    class Garage                                                     
      def initialize                                                 
        @capacity = Car.all                                          
      end                                                            
                                                                     
      attr_accessor :capacity                                        
    end                                                                                                       
    
    car1 = Car.new        
    car2 = Car.new        
    car3 = Car.new        
                          
    garage = Garage.new   
                          
    print garage.capacity   # => [#<Car:0x0000563fa14128c8>, #<Car:0x0000563fa1412918>, #<Car:0x0000563fa1412990>]
    

    Also note, I have used require_relative to include car.rb class which is in the same directory.


    Edit for comment question and Cary Swoveland suggestion:

    class Car
    end
    
    require_relative 'car' # Makes car class accessible in garage.rb 
                                                                     
    class Garage                                                     
      def initialize; end                                            
                                                                     
      def capacity                                                   
        ObjectSpace.each_object(Car).to_a                            
      end                                                            
    end                                                              
                                                                     
    car1 = Car.new                                                   
    garage = Garage.new                                              
    car2 = Car.new                                                   
                                                                     
    print garage.capacity # => [#<Car:0x000055d7bd236a00>, #<Car:0x000055d7bd236b18>]                                            
    

    Also note, I removed attr_accessor :capacity, since you don't need it in this case.