1

I have two arrays

arr1 = ["a","b","c","d"]
arr2 = ["e","f","g","h"]

and I want to get a result like this

arr3 = ["ae","bf","cg","dh"]

How can I do that in ruby?

jbr
  • 6,198
  • 3
  • 30
  • 42
delpha
  • 931
  • 1
  • 8
  • 23
  • use this arr1.map.with_index { |e, i| e + arr2[i] } as it is faster and see same question here at http://stackoverflow.com/questions/22312536/merging-only-respective-elements-of-two-arrays-in-one-array-in-ruby – Alok Anand Mar 26 '14 at 19:42

1 Answers1

2

I'd do using #zip :

arr1 = ["a","b","c","d"] 
arr2 = ["e","f","g","h"]
arr1.zip(arr2).map(&:join)
# => ["ae", "bf", "cg", "dh"]
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317