Comment ajouter un tableau à un autre tableau en Ruby et ne pas se retrouver avec un résultat multidimensionnel ?

somearray = ["some", "thing"]

anotherarray = ["another", "thing"]

somearray.push(anotherarray.flatten!)

Je m'attendais à ce que

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

Vous pouvez simplement utiliser l'opérateur + !

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]

Vous pouvez lire tout ce qui concerne la classe des tableaux ici: http://ruby-doc.org/core/classes/Array.html

Commentaires (7)

Essayez ceci, cela combinera vos tableaux en supprimant les doublons.

array1 = ["foo", "bar"]
array2 = ["foo1", "bar1"]

array3 = array1|array2

http://www.ruby-doc.org/core/classes/Array.html

Pour plus de documentation, consultez la rubrique "Set Union&quot ;

Commentaires (2)

["some&quot ;, "thing&quot ;] + ["another&quot ; + "thing&quot ;].

Commentaires (1)