Printing multi-dimensional arrays

#1

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

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

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

s.each { |sub_array| puts sub_array}

The two codes above yielded me the same results.
This lesson is trying to teach us to print each indivudal part of each element in a multi-dimensional array.

Quote "We've created a 2-D array, s (for "sandwiches"). We want to iterate overs in such a way that we don't print out each element, like ["ham", "swiss"], but each element withineach element, so we get a list of all the meats and cheeses within s. "

In the page before it was teaching us how to iterate over a one dimensional array such as
numbers = [1, 2, 3, 4, 5]
numbers.each { |element| puts element }
But what I did is I did the same method for the one dimensional array shown above on the multi-dimensional and it printed out just like it wanted me to (each individual element within each element).

Like this:
ham
swiss
turkey
cheddar
roast beef
gruyere
[["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]
So I'm getting the impression that it was expecting it to print out each first tier element as a whole like this
[ham, swiss]
[turkey, cheddar]
[roast beef, gryuere]

If the codw below doesn't do it, what would?

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

s.each { |sub_array| puts sub_array}

``Replace this line with your code.``

#2

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

sub_array = s.each
sub_array.each { |x| puts x }``````

#3

This is the method I instinctively wrote the code in, before checking the hint. It works! Can anyone explain why I shouldn't use this method?

#5

There is a good article on multi-dimensional arrays. This is useful for better visualisation when build the data:
https://processing.org/tutorials/2darray/