如何在rails中创建对象数组?

时间:2022-09-28 21:15:28

I need to know how to create object array in rails and how to add elements in to that.

我需要知道如何在rails中创建对象数组以及如何在其中添加元素。

I'm new to ruby on rails and this could be some sort of silly question but I can't find exact answer for that. So can please give some expert ideas about this

我是铁杆上的红宝石新手,这可能是某种愚蠢的问题,但我无法找到确切的答案。那么请对此提出一些专家意见

4 个解决方案

#1


12  

Since everything is an object in Ruby (including numbers and strings) any array you create is an object array that has no limits on the types of objects it can hold. There are no arrays of integers, or arrays of widgets in Ruby. Arrays are just arrays.

由于一切都是Ruby中的对象(包括数字和字符串),因此您创建的任何数组都是一个对象数组,对它可以容纳的对象类型没有限制。 Ruby中没有整数数组或小部件数组。数组只是数组。

my_array = [24, :a_symbol, 'a string', Object.new, [1,2,3]]

As you can see, an array can contain anything, even another array.

如您所见,数组可以包含任何内容,甚至包含另一个数组。

#2


17  

All you need is an array:

你需要的只是一个数组:

objArray = []
# or, if you want to be verbose
objArray = Array.new

To push, push or use <<:

推,推或使用<<:

objArray.push 17
>>> [17]

objArray << 4
>>> [17, 4]

You can use any object you like, it doesn't have to be of a particular type.

您可以使用任何您喜欢的对象,它不必是特定类型。

#3


2  

Depending on the situation, I like this construct to initialize an array.

根据具体情况,我喜欢这个构造来初始化一个数组。

# Create a new array of 10 new objects
Array.new(10) { Object.new }
#=> [#<Object:0x007fd2709e9310>, #<Object:0x007fd2709e92e8>, #<Object:0x007fd2709e92c0>, #<Object:0x007fd2709e9298>, #<Object:0x007fd2709e9270>, #<Object:0x007fd2709e9248>, #<Object:0x007fd2709e9220>, #<Object:0x007fd2709e91f8>, #<Object:0x007fd2709e91d0>, #<Object:0x007fd2709e91a8>]

#4


0  

Also if you need to create array of words next construction could be used to avoid usage of quotes:

此外,如果您需要创建单词数组,可以使用下一个构造来避免使用引号:

array = %w[first second third]

or

要么

array = %w(first second third)

#1


12  

Since everything is an object in Ruby (including numbers and strings) any array you create is an object array that has no limits on the types of objects it can hold. There are no arrays of integers, or arrays of widgets in Ruby. Arrays are just arrays.

由于一切都是Ruby中的对象(包括数字和字符串),因此您创建的任何数组都是一个对象数组,对它可以容纳的对象类型没有限制。 Ruby中没有整数数组或小部件数组。数组只是数组。

my_array = [24, :a_symbol, 'a string', Object.new, [1,2,3]]

As you can see, an array can contain anything, even another array.

如您所见,数组可以包含任何内容,甚至包含另一个数组。

#2


17  

All you need is an array:

你需要的只是一个数组:

objArray = []
# or, if you want to be verbose
objArray = Array.new

To push, push or use <<:

推,推或使用<<:

objArray.push 17
>>> [17]

objArray << 4
>>> [17, 4]

You can use any object you like, it doesn't have to be of a particular type.

您可以使用任何您喜欢的对象,它不必是特定类型。

#3


2  

Depending on the situation, I like this construct to initialize an array.

根据具体情况,我喜欢这个构造来初始化一个数组。

# Create a new array of 10 new objects
Array.new(10) { Object.new }
#=> [#<Object:0x007fd2709e9310>, #<Object:0x007fd2709e92e8>, #<Object:0x007fd2709e92c0>, #<Object:0x007fd2709e9298>, #<Object:0x007fd2709e9270>, #<Object:0x007fd2709e9248>, #<Object:0x007fd2709e9220>, #<Object:0x007fd2709e91f8>, #<Object:0x007fd2709e91d0>, #<Object:0x007fd2709e91a8>]

#4


0  

Also if you need to create array of words next construction could be used to avoid usage of quotes:

此外,如果您需要创建单词数组,可以使用下一个构造来避免使用引号:

array = %w[first second third]

or

要么

array = %w(first second third)