a = [3, 4, 7, 8, 3]
b = [5, 3, 6, 8, 3]
Assuming arrays of same length, is there a way to use each or some other idiomatic way to get a result from each element of both arrays? Without using a counter?
For example, to get the product of each element: [15, 12, 42, 64, 9]
(0..a.count - 1).each do |i| is so ugly…
Ruby 1.9.3
For performance reasons,
zipmay be better, buttransposekeeps the symmetry and is easier to understand.A difference between
zipandtransposeis that in case the arrays do not have the same length, the former insertsnilas a default whereas the latter raises an error. Depending on the situation, you might favor one over the other.