revolver
revolver

Reputation: 2405

merging 2 array in ruby

I have 2 arrays. For example

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

How do I merge them so that I have an array like below

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

Upvotes: 1

Views: 102

Answers (2)

Shamith c
Shamith c

Reputation: 3739

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"]] 

Upvotes: 0

pguardiario
pguardiario

Reputation: 55002

The short answer is.....

x.zip y

Upvotes: 6

Related Questions