我试着:

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

我预期的

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

但有

["some", "thing", nil]

当前回答

适用于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]

其他回答

(array1 + array2).uniq

这样你就可以先得到array1元素。你不会得到副本。

你可以使用+运算符!

irb(main):001:0> a = [1,2]
=> [1, 2]
irb(main):002:0> b = [3,4]
=> [3, 4]
irb(main):003:0> a + b
=> [1, 2, 3, 4]

你可以在这里阅读关于数组类的所有内容: http://ruby-doc.org/core/classes/Array.html

Somearray = ["some", "thing"]

Anotherarray = ["another", "thing"]

Somearray + anotherarray

["some", "thing"] + ["another", "thing"]
somearray = ["some", "thing"]
anotherarray = ["another", "thing"]
somearray + anotherarray # => ["some", "thing", "another", "thing"]
somearray.concat anotherarray # => ["some", "thing", "another", "thing"]
somearray.push(anotherarray).flatten # => ["some", "thing", "another", "thing"]
somearray.push *anotherarray # => ["another", "thing", "another", "thing"]