Just for the record, a benchmark with the different listed solutions. Results:
user system total real
Map with index 1.120000 0.000000 1.120000 ( 1.113265)
Each with index and Map 1.370000 0.000000 1.370000 ( 1.375209)
Zip and Map {|a|} 1.950000 0.000000 1.950000 ( 1.952049)
Zip and Map (&:) 1.980000 0.000000 1.980000 ( 1.980995)
Transpose and Map (&:) 1.970000 0.000000 1.970000 ( 1.976538)
Benchmark
require 'benchmark'
N = 1_000_000
A = ["a","b","c"]
B = ["d","e","f"]
Benchmark.bmbm(20) do |x|
x.report("Map with index") do
N.times do |index|
A.map.with_index { |e, i| e + B[i] }
end
end
x.report("Each with index and Map") do
N.times do |index|
A.each_with_index.map { |e, i| e + B[i] }
end
end
x.report("Zip and Map {|a|}") do
N.times do |index|
A.zip(B).map { |a| a.join }
end
end
x.report("Zip and Map (&:)") do
N.times do |index|
A.zip(B).map(&:join)
end
end
x.report("Transpose and Map (&:)") do
N.times do |index|
[A,B].transpose.map(&:join)
end
end
end