DEV Community

Amanda Mendez
Amanda Mendez

Posted on

Ruby newbie time: Grasping "array-ception" in Ruby

Being fairly new to programming, I start struggling following the logic of the code once I hit topics like loops, arrays, or hash. Not to mention when they all start to combine. Then my head kind of just starts exploding. 🤯

Today I'm learning Ruby and worked on this (courtesy Codeacademy's Learn Ruby course):

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

s.each do | sub_array |
  sub_array.each do | y |
    puts y
  end
end
Enter fullscreen mode Exit fullscreen mode

To try to understand it better I'll try to break it down starting with the first part:

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

Enter fullscreen mode Exit fullscreen mode

So the above is what I've dubbed "array-ception". There are three inner-arrays within a larger outer array. Those inner arrays are some run-of-the-mill sandwich combinations: ["meat, "cheese"].

Ok. Makes sense so far right? But what about when we want to start doing things with these "array-ceptions"? For example in our initial code snippet, we're trying to write a simple program where we can just print every element of each array.

I can't be the only beginner who has ever thought, "Where do I even start?", "What's going on?", or "All this thinking about sandwiches is making me hungry."

I suppose since there are kind of two parts, the larger outer array and the smaller inner arrays, we should start tackling the outer array first. I think that's what comes next in the code anyway:

s.each do |sub_array|
Enter fullscreen mode Exit fullscreen mode

If you're like me, this is where it starts to get confusing. If there's anything I've learned, it's that when things are confusing, they're begging to be broken down even further.


Part 1

So let's start with the variable 's'. It seems like 's' is kind of a good way to keep track of that larger outer array we talked about earlier. So that means that 's.each' probably means something like "For each element in the larger outer array called s, do something..."

Ok great! Making more sense now!

Now we just have to contend with the pesky 'do |sub_array|' part. So what does it mean? What are we doing?

Jack Skellington What does it mean gif

Part 2

Well, codeacademy did a great job by naming the variable 'sub_array' because we can assume that to mean we're going to start tackling the inner arrays I mentioned earlier. So that means |sub_array| is pointing us to each smaller array like '["ham", "swiss"]'.

Alright so that's making more sense. Seems like that part we can assume to mean something like 'do something to the inner arrays...'

So putting parts 1 and 2 together means "For each element in the larger outer array called s, do something to the inner arrays called sub_array..."

Woohoo! I think we got the first part down.

Part 3

Now that that's cleared up, we can tackle the next part:

sub_array.each do |y|
Enter fullscreen mode Exit fullscreen mode

Hmmm... this should be looking familiar. 🤔 Actually now that I look at it it's basically exactly the same except for the variables named. So instead of doing something to the larger array, we're now starting with the inner arrays or sub arrays.

What's confusing to me here is '|y|'. What is it?? Where did it come from? To make it easier to follow in my head, I'd probably change the variable name to something like 'smallest_element' because here I think 'y' is just tracking each inner element of the sub arrays. That's the smallest part the arrays can be broken down into.

So I'd think of it more like this: sub_array.each do |smallest_element|. And to translate that to plain English, it'd be something like, "For each sub array do something to the smallest element in the array."


Cool! I think we can put it all together now.

So parts 1-3 would translate to something like. "For each element in the larger outer array called s, do something to the inner arrays called sub_array. What are we doing to the inner arrays? Well, for each inner array, do something to the smallest element in that array called y."

Ok. I can follow the logic so far, but I haven't actually done the big 'something' that we've been alluding too. The anticipation is killing me. What are we going to do to those smaller elements?

    puts y
Enter fullscreen mode Exit fullscreen mode

That's where the last part of the code comes in. It's the final flourish of our little program. We're going to simply 'puts y' meaning we want to display each of the smallest elements of the array on a new line.

And that's it! The output should look like this:

ham
swiss
turkey
cheddar
roast beef
gruyere
Enter fullscreen mode Exit fullscreen mode

Hopefully that helped you out if you were having trouble following the logic. It certainly helped me to try to type it out to explain it anyway.

And keep in mind I'm a beginner and learning along with you. Or maybe you're more advanced, in which case, don't hesitate to correct me if I've made an error.

See you in the next post!

Top comments (0)