Overview
It is possible to traverse three arrays at once in Ruby. It can be done using the .zip function. Here is the doc link for the zip function
https://ruby-doc.org/core-2.0.0/Array.html#method-i-zip
Let’s see the program for the same
Program
Here is the code to traverse three arrays together when the size of all the three arrays is the same
a = ["d", "e", "f"]
b = ["g", "h", "i"]
["a", "b", "c"].zip(a, b) do |x,y,z|
puts x+ " " + y + " " + z
end
Output
a d g
b e h
c f i
If the size of the arrays is not the same then it will print empty space for elements in arrays of lesser size
a = ["d", "e"]
b = ["g"]
["a", "b", "c"].zip(a, b) do |x,y,z|
puts x
puts y
puts z
end
Output
a
d
g
b
e
c
Note: Check out our system design tutorial series System Design Questions