12 Why the Nested .each do ||?


#1

This is what the hint says the structure should be

s.each do |sub_array|
sub_array.each do |y|
puts y
end
end

And this is what I had

s.each do |sub_array|
puts sub_array
end

Both ended up producing the same result so I am confused on what the nested "sub_array.each do |y|" is for


#2

With your code you are printing out each sub_array of the main array not the individual things in the sub array.

lets say you have
[ [1,2,3,4], [5,2,3,4] ]
this will print out with your code as [1,2,3,4] [5,2,3,4]

With their code

s.each do |sub_array|
sub_array.each do |y|
puts y
end
end

[ [1,2,3,4], [5,2,3,4] ]
should print out as 1,2,3,4,5,2,3,4

This is only noticeable if you change

s.each do |sub_array|
puts sub_array
end

to

s.each do |sub_array|
puts "#{sub_array}"
end

the difference between your loop and codeacademy's loop is subtle but very important to understand.

they are pretty much going one step deeper and grabbing each element of each sub-array while your just grabbing the sub-array :slightly_smiling: .

I hope that makes sense and helps you out a little :sunny: