r/learnruby • u/Lucidio • Jul 26 '21
I don't understand this CodeAcadamy Solution!
Hey, I'm going through ruby. I have some rudimentary skill with javascript, nothing fancy, still gotta refer to docs for most things.
That said, I'm on the section "Iterating Over Multidimensional Arrays" where I must
Puts out every element inside the sub-arrays inside s. Iterate through .each element in the s array. Call the elements sub_array. Then iterate through .each sub_array and puts out their items.
The solution is:
s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]
sub_array = 0
s.each do |x| sub_array = x
sub_array.each do |y| puts y end
endMy solution that didn't work was:
s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]
s.each { |x, y| puts "#{x}" "#{y}" }It didn't work. I know I missed the sub_array part but do not understand how to integrate it or what's happening....
View all comments
Show parent comment
1
u/The-more-you-gnoll Jul 27 '21
Don't worry, you'll get there. A lot of this stuff is not very intuitive at first. You just have to keep working with it and eventually it'll snap into place.