用元素填充数组N次

时间:2021-09-10 21:42:09

I want to fill an array with 1 element but 5 times. What I got so far.

我想用1个元素填充一个数组但是5次。到目前为止我得到了什么。

str = 1234
a = []

5.times { a << str }
puts a # => 1234, 1234, 1234, 1234, 1234

It works but this feels not the ruby way. Can someone point me in the right direction to init an array with 5 times the same value?

它有效,但这不是红宝石的方式。有人能指出我正确的方向来初始化一个具有5倍相同值的数组吗?

4 个解决方案

#1


40  

Array.new(5, str)
# => [1234, 1234, 1234, 1234, 1234]

By the way, it is a bad practice to name a variable str that is assigned the value 1234. It is confusing.

顺便说一句,命名一个赋值为1234的变量str是一种不好的做法。这很令人困惑。

#2


17  

This should work:

这应该工作:

[1234] * 5
# => [1234, 1234, 1234, 1234, 1234]

#3


4  

Although the accepted answer is fine in the case of strings and other immutable objects, I think it's worth expanding on Max's comment about mutable objects.

尽管在字符串和其他不可变对象的情况下接受的答案是正确的,但我认为值得扩展Max对可变对象的评论。

Let's say you want to fill an array of elements with 5 empty hashes:

假设您想要用5个空哈希填充元素数组:

xs = Array.new(3) { {} }

The above will return an array of the following:

以上将返回以下数组:

[{}, {}, {}]

If you then modify the first element of the array:

如果您然后修改数组的第一个元素:

xs.first[:hello] = "world"

Only the first element will be modified.

只会修改第一个元素。

xs # => [{ hello: "world" }, {}, {}]

On the other hand, using Array.new(3, {}) will yield an array with all elements pointing to the same hash:

另一方面,使用Array.new(3,{})将产生一个数组,其中所有元素都指向相同的哈希:

xs = Array.new(3, {}) # => [{}, {}, {}]
xs.first[:hello] = "world"
xs # => [{ hello: "world" }, { hello: "world" }, { hello: "world" }]

which is probably not the intended result.

这可能不是预期的结果。

#4


0  

You can fill the array like this:

您可以像这样填充数组:

a = []
=> []

a.fill("_", 0..5)
=> ["_", "_", "_", "_", "_"]

#1


40  

Array.new(5, str)
# => [1234, 1234, 1234, 1234, 1234]

By the way, it is a bad practice to name a variable str that is assigned the value 1234. It is confusing.

顺便说一句,命名一个赋值为1234的变量str是一种不好的做法。这很令人困惑。

#2


17  

This should work:

这应该工作:

[1234] * 5
# => [1234, 1234, 1234, 1234, 1234]

#3


4  

Although the accepted answer is fine in the case of strings and other immutable objects, I think it's worth expanding on Max's comment about mutable objects.

尽管在字符串和其他不可变对象的情况下接受的答案是正确的,但我认为值得扩展Max对可变对象的评论。

Let's say you want to fill an array of elements with 5 empty hashes:

假设您想要用5个空哈希填充元素数组:

xs = Array.new(3) { {} }

The above will return an array of the following:

以上将返回以下数组:

[{}, {}, {}]

If you then modify the first element of the array:

如果您然后修改数组的第一个元素:

xs.first[:hello] = "world"

Only the first element will be modified.

只会修改第一个元素。

xs # => [{ hello: "world" }, {}, {}]

On the other hand, using Array.new(3, {}) will yield an array with all elements pointing to the same hash:

另一方面,使用Array.new(3,{})将产生一个数组,其中所有元素都指向相同的哈希:

xs = Array.new(3, {}) # => [{}, {}, {}]
xs.first[:hello] = "world"
xs # => [{ hello: "world" }, { hello: "world" }, { hello: "world" }]

which is probably not the intended result.

这可能不是预期的结果。

#4


0  

You can fill the array like this:

您可以像这样填充数组:

a = []
=> []

a.fill("_", 0..5)
=> ["_", "_", "_", "_", "_"]