在ruby中合并2个数组

时间:2012-07-16 06:48:40

标签: ruby arrays

我有2个数组。例如

x= [1,2,3,4,5]
y= [a,b,c,d,e]

如何合并它们以便我有一个如下所示的数组

z=[[1,a],[2,b],[3,c],[4,d],[5,e]]

2 个答案:

答案 0 :(得分:6)

简短的回答是......

x.zip y

答案 1 :(得分:0)

1.9.3p194 :011 >   x= [1,2,3,4,5]
 => [1, 2, 3, 4, 5] 
1.9.3p194 :012 > y= ['a','b','c','d','e']
 => ["a", "b", "c", "d", "e"] 
1.9.3p194 :013 > x.zip(y)
 => [[1, "a"], [2, "b"], [3, "c"], [4, "d"], [5, "e"]]