在一个语句中为一个多个对象分配一个值

时间:2023-01-31 15:12:03

If I want to declare three new arrays (a1, a2, a3), I can do this:

如果我想声明三个新数组(a1,a2,a3),我可以这样做:

a1=[]
a2=[]
a3=[]

But now I want to do it all on one line, like

但现在我想在一条线上完成这一切,比如

a1, a2, a3 = []

but this fails. How can I assign them all to an empty array on one line?

但这失败了。如何将它们全部分配到一行上的空数组?

7 个解决方案

#1


18  

To do something similar to your second example, you would still need to create three arrays:

要做与第二个示例类似的操作,您仍然需要创建三个数组:

a1, a2, a3 = [], [], []

#2


5  

If you insist on not repeating the [] literal, then one way is:

如果你坚持不重复[]文字,那么一种方法是:

a1, a2, a3 = Array.new(3){[]}

Another way is:

另一种方式是:

a1 = (a2 = (a3 = []).dup).dup

#3


4  

As Andrew Marshall pointed out, there is yet another, even shorter way of assigning one value to multiple objects.

正如Andrew Marshall指出的那样,还有另一种更简单的方法是将一个值分配给多个对象。

a1, a2, a3 = [42] * 3

#4


3  

Ruby Multiple assignment is unexpected in below situations:

Ruby在以下情况下,多次分配是意外的:

a1, a2, a3 = []

Above code will not assign blank array in any array variable.

上面的代码不会在任何数组变量中分配空数组。

use,

a1, a2, a3 = [], [], []

You need to provide number of values as number of variable are used to initialize.

您需要提供多个值作为变量数用于初始化。

#5


3  

These answers are over-engineered, focused on the misleading use of Array in the OP's sample.

这些答案是过度设计的,侧重于OP样本中误导性使用Array。

a1 = a2 = a3 = []
# => []

Test the results

测试结果

a1
# => []
a2
# => []
a3
# => []
a4 # for sake of comparison
NameError: undefined local variable or method `a4' for main:Object

#6


0  

Will the following snippet behave in the same way on all ruby VMs?

以下代码段是否会在所有ruby VM上以相同的方式运行?

a,b = b,nil

The point of this one liner is to set 'a' to a value from 'b' and reset 'b'.

这一个班轮的要点是将'a'设置为'b'的值并重置'b'。

#7


0  

You can also do the following:

您还可以执行以下操作:

a1=[]; a2=[]; a3=[]

#1


18  

To do something similar to your second example, you would still need to create three arrays:

要做与第二个示例类似的操作,您仍然需要创建三个数组:

a1, a2, a3 = [], [], []

#2


5  

If you insist on not repeating the [] literal, then one way is:

如果你坚持不重复[]文字,那么一种方法是:

a1, a2, a3 = Array.new(3){[]}

Another way is:

另一种方式是:

a1 = (a2 = (a3 = []).dup).dup

#3


4  

As Andrew Marshall pointed out, there is yet another, even shorter way of assigning one value to multiple objects.

正如Andrew Marshall指出的那样,还有另一种更简单的方法是将一个值分配给多个对象。

a1, a2, a3 = [42] * 3

#4


3  

Ruby Multiple assignment is unexpected in below situations:

Ruby在以下情况下,多次分配是意外的:

a1, a2, a3 = []

Above code will not assign blank array in any array variable.

上面的代码不会在任何数组变量中分配空数组。

use,

a1, a2, a3 = [], [], []

You need to provide number of values as number of variable are used to initialize.

您需要提供多个值作为变量数用于初始化。

#5


3  

These answers are over-engineered, focused on the misleading use of Array in the OP's sample.

这些答案是过度设计的,侧重于OP样本中误导性使用Array。

a1 = a2 = a3 = []
# => []

Test the results

测试结果

a1
# => []
a2
# => []
a3
# => []
a4 # for sake of comparison
NameError: undefined local variable or method `a4' for main:Object

#6


0  

Will the following snippet behave in the same way on all ruby VMs?

以下代码段是否会在所有ruby VM上以相同的方式运行?

a,b = b,nil

The point of this one liner is to set 'a' to a value from 'b' and reset 'b'.

这一个班轮的要点是将'a'设置为'b'的值并重置'b'。

#7


0  

You can also do the following:

您还可以执行以下操作:

a1=[]; a2=[]; a3=[]