I think this shouldn't work?



Hi folks,

I am working on lesson (Re)Introduction to Iteration. I had no idea how to "Puts out every element inside the sub-arrays inside s." so I just went for the first thing that came to mind and hit Submit:

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

To my surprise it worked in the console, and the website gave me the Way to Go! message.
But when I check the hints it says the correct answer looks more like:

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

What's going on here, and what am I missing?
Any explanation is much appreciated.


Have you compared the output of each procedure? What is the difference, and why?

Read carefully...

puts out every element inside the sub-arrays inside s.

The idea behind the lesson, despite the lax SCT, is to explore nesting of blocks / do..end. The hint gives a clear example of nesting.

We could use blocks instead, to the same end...

s.each { |s_sub_x| s_sub_x.each { |x| puts x } }


sometimes this happens with the tests, you pass yet still are not getting the correct result. they most likely have their test set to ensure you have returned the elements of the array, which you did ( for the parent array). you returned elements "sub arrays" which you still need to iterate through though. computers are not perfect and neither are tests. they should have probably set it to check for a puts of each individual string in the sub array instead of each element of an array.


Thanks for the replies.

mtf; I get the same output from both procedures - that's why I thought something was wrong.
What should the difference be?
Thanks for the extra tip.

cfecteau1; That makes sense. I usually try to solve a question myself first, and only when it doesn't work resort to the Hints - better way to learn, in my opinion - but if the site tells me my answer is correct even when it's not, my strategy isn't a very good one. Shall I just assume this is a rare occurence?


@webpro13403 if you want to see the difference in the 2 iterations use p instead of puts this will return subarrays instead of individual elements. since your using puts it outputs the elements on a seperate line. Same would happen if you run an array output using puts without iterating. And yes it is rare but it happens, most of the time its because of little things such as how you set an output. In some ways its good because it makes you look a little deeper to figure out the why.


Without actually testing, I was under the assumption that the output for the first example,

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

would be,

['ham', 'swiss']
['turkey', 'cheddar']
['roast beef', 'gruyere']

Well, we know what assume does. I was wrong. The output looks like this,

roast beef

I'm so used to thinking about the mechanics that I miss the small details in Ruby. My bad, and my apologies.


i thought the same thing until i ran it through my terminal. i didn't think puts would iterate through an array on it's own. kind of interesting to see though, makes you realize how important the output method can be.


Above and beyond the call of duty. Thanks a lot, fellas!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.