12/16 Iterating over multi-dimensional arrays - why does this work?


#1


https://www.codecademy.com/courses/ruby-beginner-en-F3loB/2/3?curriculum_id=5059f8619189a5000201fbcb#

Both scenarios work here. Is Scenario 2 simply breaking apart what Scenario 1 is doing?


_**Scenario 1:**_

s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]
s.each {|sub_array| puts sub_array}

**vs.**

**_Scenario 2:_**

s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]

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


#2

@scriptmaster67781,
The confusion is due to how the =puts= display's an =array= (each array-Element on a new line)
Try to use

s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]
s.each {|sub_array| puts "#{sub_array}" }

#**vs.**
puts "================"
#**_Scenario 2:_**

s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]

s.each do |sub_array|
    sub_array.each do |x|
       puts x
    end
    puts "====== next sub_array ======"
end

#3

This worked for me.

s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]
s.each do | sub_array |
sub_array.each do | y |
puts y
end
end


#4