Ruby:对象/类数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14532844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby: Array of Objects/Classes
提问by Chris Cruz
I am not a ruby expert and this is giving me trouble. But how Would I go about creating an array of objects/classes in ruby? How would initialize it/declare it? Thanks in advance for the help.
我不是红宝石专家,这给我带来了麻烦。但是我将如何在 ruby 中创建一组对象/类?如何初始化/声明它?在此先感谢您的帮助。
This is my class, and I want to create an array of it:
这是我的班级,我想创建一个它的数组:
class DVD
attr_accessor :title, :category, :runTime, :year, :price
def initialize()
@title = title
@category = category
@runTime = runTime
@year = year
@price = price
end
end
回答by rik.vanmechelen
Ruby is duck typed (dynamic typing) And almost everything is an object, so you can just add any object to an array. For example:
Ruby 是鸭子类型(动态类型)并且几乎所有东西都是对象,因此您可以将任何对象添加到数组中。例如:
[DVD.new, DVD.new]
will create an array with 2 DVDs in it.
将创建一个包含 2 张 DVD 的阵列。
a = []
a << DVD.new
will add the DVD to the array. Check the Ruby API for the full list of array functions.
将 DVD 添加到阵列中。查看Ruby API 以获取数组函数的完整列表。
Btw, if you want to keep a list of all the DVD instances in the DVD class you can do this with a class variable, and add it to that array when you create a new DVD object.
顺便说一句,如果您想保留 DVD 类中所有 DVD 实例的列表,您可以使用类变量执行此操作,并在创建新 DVD 对象时将其添加到该数组中。
class DVD
@@array = Array.new
attr_accessor :title, :category, :runTime, :year, :price
def self.all_instances
@@array
end
def initialize()
@title = title
@category = category
@runTime = runTime
@year = year
@price = price
@@array << self
end
end
now if you do
现在如果你这样做
DVD.new
you can get the list of all the DVDs you have created so far:
您可以获得迄今为止您创建的所有 DVD 的列表:
DVD.all_instances
回答by Matheus Moreira
In order to create an array of objects in Ruby:
为了在 Ruby 中创建对象数组:
Create the array and bind it to a name:
array = []Add your objects to it:
array << DVD.new << DVD.new
创建数组并将其绑定到一个名称:
array = []将您的对象添加到其中:
array << DVD.new << DVD.new
You can add any object to an array, at any time.
您可以随时将任何对象添加到数组中。
If you wish to have access to every instance of the DVDclass, then you can rely on ObjectSpace:
如果您希望访问DVD类的每个实例,那么您可以依赖ObjectSpace:
class << DVD
def all
ObjectSpace.each_object(self).entries
end
end
dvds = DVD.all
By the way, the instance variables are not being initialized correctly.
顺便说一下,实例变量没有被正确初始化。
The following method call:
以下方法调用:
attr_accessor :title, :category, :run_time, :year, :price
Automatically creates attribute/attribute=instance methods to get and set the value of the instance variables.
自动创建attribute/attribute=实例方法来获取和设置实例变量的值。
The initializemethod, as defined:
该initialize方法,如定义:
def initialize
@title = title
@category = category
@run_time = run_time
@year = year
@price = price
end
Sets the instance variables, despite taking no arguments. What effectively happens is:
设置实例变量,尽管不带参数。有效发生的是:
- The
attributereader method is called - It reads the unset variable
- It returns
nil nilbecomes the value of the variable
- 该
attribute阅读器方法被称为 - 它读取未设置的变量
- 它返回
nil nil成为变量的值
What you want to do is pass the values of the variables to the initializemethod:
您想要做的是将变量的值传递给initialize方法:
def initialize(title, category, run_time, year, price)
# local variables shadow the reader methods
@title = title
@category = category
@run_time = run_time
@year = year
@price = price
end
DVD.new 'Title', :action, 90, 2006, 19.99
Also, if the only required attribute is the DVD's title then you can do it this way:
此外,如果唯一必需的属性是DVD的标题,那么您可以这样做:
def initialize(title, attributes = {})
@title = title
@category = attributes[:category]
@run_time = attributes[:run_time]
@year = attributes[:year]
@price = attributes[:price]
end
DVD.new 'Second'
DVD.new 'Third', price: 29.99, year: 2011
回答by Fanda
two_DVD = Array.new(2){DVD.new}
two_DVD = Array.new(2){DVD.new}

