我试着:

somearray = ["some", "thing"]
anotherarray = ["another", "thing"]
somearray.push(anotherarray.flatten!)

我预期的

["some", "thing", "another", "thing"]

但有

["some", "thing", nil]

当前回答

a = ["some", "thing"]
b = ["another", "thing"]

将b追加到a并将结果存储在a中:

a.push(*b)

or

a += b

在这两种情况下,a都变成:

["some", "thing", "another", "thing"]

但在前一种情况下,b的元素被追加到现有的a数组中,在后一种情况下,两个数组连接在一起,结果存储在a中。

其他回答

Somearray = ["some", "thing"]

Anotherarray = ["another", "thing"]

Somearray + anotherarray

适用于Ruby版本>= 2.0但不适用于旧版本的简单方法:

irb(main):001:0> a=[1,2]
=> [1, 2]
irb(main):003:0> b=[3,4]
=> [3, 4]
irb(main):002:0> c=[5,6]
=> [5, 6]
irb(main):004:0> [*a,*b,*c]
=> [1, 2, 3, 4, 5, 6]
a = ["some", "thing"]
b = ["another", "thing"]

将b追加到a并将结果存储在a中:

a.push(*b)

or

a += b

在这两种情况下,a都变成:

["some", "thing", "another", "thing"]

但在前一种情况下,b的元素被追加到现有的a数组中,在后一种情况下,两个数组连接在一起,结果存储在a中。

["some", "thing"] + ["another", "thing"]

只是另一种方法。

[somearray, anotherarray].flatten
=> ["some", "thing", "another", "thing"]