Web Development

Loops are one of the first things you learn in programming. Here is a way to loop through multiple arrays in ruby.

first = Array(1..16)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
second = Array(19..34)
=> [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]

Loop through 2 arrays in Ruby

first.zip(second).each {|b| puts b}
=> [[1, 19],
 [2, 20],
 [3, 21],
 [4, 22],
 [5, 23],
 [6, 24],
 [7, 25],
 [8, 26],
 [9, 27],
 [10, 28],
 [11, 29],
 [12, 30],
 [13, 31],
 [14, 32],
 [15, 33],
 [16, 34]]

Loop through 3 arrays in Ruby

first = Array(1..16)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
second = Array(19..34)
=> [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
third = Array(37..52)
=> [37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]
first.zip(second, third).each {|b| puts b}
=> [[1, 19, 37],
 [2, 20, 38],
 [3, 21, 39],
 [4, 22, 40],
 [5, 23, 41],
 [6, 24, 42],
 [7, 25, 43],
 [8, 26, 44],
 [9, 27, 45],
 [10, 28, 46],
 [11, 29, 47],
 [12, 30, 48],
 [13, 31, 49],
 [14, 32, 50],
 [15, 33, 51],
 [16, 34, 52]]

Voila!