OneBite.Dev - Coding blog in a bite size

implement a two nested loop in Ruby

Code snippet on how to implement a two nested loop in Ruby

for i in 1..3
  for j in 1..3
    puts "#{i}, #{j}"
  end
end

This code will print out all possible pairs of numbers between 1 and 3. The first ‘for’ loop will establish the outer loop and create a variable (‘i’) that will cycle between 1 and 3. The second ‘for’ loop creates a second variable (‘j’) and cycles this between 1 and 3. The ‘puts’ command will then print out the two variables combined with a comma and space in between. After this is done it will start the second loop again, until all possible combinations of the two variables have been looped over.

ruby