mann
mann

Reputation: 184

iteration inside another iteration?

I’m trying to output sets of numbers that look like this:

0,0
0,1
0,2
0,3
0,4
0,5
.
.
.
1,0
1,1
1,2
1,3
1,4
1,5
.
.
.

I’m having trouble iterating the second column while leaving the first column the same for ten iterations before moving from 0 to 1 in the first column.

I tried:

(0..9).each do |num|
  number = num
  number_plus = num
  puts "#{number}, #{number_plus}"
  number_plus = num + 1
  puts "#{number}, #{number_plus}"
end

which outputs this, closer but I'm still missing something related to a nested iterator:

0, 0
0, 1
1, 1
1, 2
2, 2
2, 3
3, 3
3, 4
4, 4
4, 5
5, 5
5, 6
6, 6
6, 7
7, 7
7, 8
8, 8
8, 9
9, 9
9, 10

Any guidance would be great.

Upvotes: 1

Views: 93

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

Putting the second iteration inside the first is one possible solution:

(0..9).each do |num1|
  (0..9).each do |num2|
    puts "#{num1}, #{num2}"
  end
end

Upvotes: 1

Related Questions