如何将类的实例添加到另一个类(Ruby)的数组中

发布于 2025-01-23 14:05:54 字数 815 浏览 0 评论 0原文

我的目录中有两个文件,一个是garage.rb&另一个称为car.rb

car.rb文件只保存一个汽车对象:

class Car
end

garage.rb文件如下:

require 'car.rb' #Makes car class accessible in garage.rb

class Garage
  def initialize
    @capacity = []
  end

  attr_accessor :capacity

end

当我通过调用car的新实例时car = car.new,如何将car对象放在@capacity array array my my默认值中?

本质上,每当我调用car = car.new时,我都希望将car立即放在@capacity array中。

我了解我可以做出这样的函数:

def add_car_to_garage(car)
  capacity << car
end

但是,我希望汽车在创建车库时在车库中启动,所以我不希望方法将其添加到数组中,我只希望它自动从此开始创建CAR的实例。

任何建议将不胜感激。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2025-01-30 14:05:54

您可以使用objectspace every_object CAR类中的方法定义方法以跟踪每个生活实例,例如:

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>]

还请注意,我使用require_relative来包括<代码> car.rb 类别在同一目录中。


编辑评论问题和Cary Swoveland建议:

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>]                                            

另外,我删除了attr_accessor:apcation,因为在这种情况下您不需要它。

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.

梦太阳 2025-01-30 14:05:54

CAR的实例收集应由该类保留和维护,并根据需要提供给其他类。可以如下完成。

class Car
  @cars = []

  class << self
    attr_reader :cars
  end

  def initialize
    self.class.cars << self
  end
  
  def self.remove_car(car)
    cars.delete(car)
  end
end
class Garage
  def self.cars
    Cars.cars
  end
end

创建类car初始化类实例变量@cars并为其创建一个getter,cars :: cars :: cars,后者通过调用attr_reader on car的Singleton类。

让我们创建一辆汽车:

car1 = Car.new
  #=> #<Car:0x00007fcf3db76fd8>

然后

Car.cars
  #=> [#<Car:0x00007fcf3db76fd8>]
Garage.cars
  #=> [#<Car:0x00007fcf3db76fd8>]

创建另一辆车。

car2 = Car.new
  #=> #<Car:0x00007fcf3db5c9a8>

然后

Car.cars
  #=> [#<Car:0x00007fcf3db76fd8>, #<Car:0x00007fcf3db5c9a8>]
Garage.cars
  #=> [#<Car:0x00007fcf3db76fd8>, #<Car:0x00007fcf3db5c9a8>]

卸下汽车。

Car.remove_car(car1)
  #=> #<Car:0x00007fcf3db76fd8>

然后,

Car.cars
  #=> [#<Car:0x00007fcf3db5c9a8>]
Garage.cars
  #=> [#<Car:0x00007fcf3db5c9a8>]

要使类实例变量@cars可用于车库添加一个简单实例方法的实例:

class Garage
  def all_cars
    self.class.cars
  end                                                            
end
Garage.new.all_cars
  #=> [#<Car:0x00007fcf3db5c9a8>]

in car

class << self
  attr_reader :cars
end

可以写作。

singleton_class.public_send(:attr_reader, :cars)

The collection of instances of Car should be kept and maintained by that class and made available to other classes as needed. That can be done as follows.

class Car
  @cars = []

  class << self
    attr_reader :cars
  end

  def initialize
    self.class.cars << self
  end
  
  def self.remove_car(car)
    cars.delete(car)
  end
end
class Garage
  def self.cars
    Cars.cars
  end
end

The creation of the class Car initializes the class instance variable @cars and creates a getter for it, Cars::cars, the latter by invoking attr_reader on Car's singleton class.

Let's create a car:

car1 = Car.new
  #=> #<Car:0x00007fcf3db76fd8>

Then

Car.cars
  #=> [#<Car:0x00007fcf3db76fd8>]
Garage.cars
  #=> [#<Car:0x00007fcf3db76fd8>]

Create another car.​

car2 = Car.new
  #=> #<Car:0x00007fcf3db5c9a8>

Then

Car.cars
  #=> [#<Car:0x00007fcf3db76fd8>, #<Car:0x00007fcf3db5c9a8>]
Garage.cars
  #=> [#<Car:0x00007fcf3db76fd8>, #<Car:0x00007fcf3db5c9a8>]

Now remove a car.

Car.remove_car(car1)
  #=> #<Car:0x00007fcf3db76fd8>

Then

Car.cars
  #=> [#<Car:0x00007fcf3db5c9a8>]
Garage.cars
  #=> [#<Car:0x00007fcf3db5c9a8>]

To make the class instance variable @cars available to instances of Garage add a simple instance method:

class Garage
  def all_cars
    self.class.cars
  end                                                            
end
Garage.new.all_cars
  #=> [#<Car:0x00007fcf3db5c9a8>]

In Car

class << self
  attr_reader :cars
end

can alternatively be written

singleton_class.public_send(:attr_reader, :cars)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文