The Starting Four of Ruby’s Enumerable Methods: .each, .map, .select and .find

Mary Ann Navarrete
Dec 21, 2020 · 7 min read

Even though an NBA team has five starters, for the purposes of explaining the power-players of Ruby enumerable methods, we have four starters. These four are the most powerful and useful enumerable mixins of the group. So, what does the NBA have anything to do with a Ruby enumerable? Aside from the fact that I’m a huge NBA fan, and this week is the start of the 2020–2021 season, let me give you a little backstory.

I started as a full-time student at Flatiron School’s Software Engineering Bootcamp five weeks ago. My biggest struggle those first three weeks was iterating through arrays and hashes to get the desired result or value. Trying to figure out which method to use just wasn’t clicking for me. I would end up using .each until I exhausted its possibilities, then go down the list of other methods until I got one to work. I failed my first code challenge because I couldn’t get past why I wasn’t getting the right result on one question when I knew the other ones on the test. That evening, I redid my code challenge on my own and got everything working perfectly. It was just a matter of timing.

Then in the 3rd week, we had our first partner project. My partner and I decided to go the data analytics route, analyzing the statistics of NBA teams and players. I mean, for someone who claims to love NBA data, not knowing which methods to use for iterating such data instantly was tough. My excitement to do this project soon became anxiety-ridden, as I felt my lack of quick decisiveness held me back. I had this sense of guilt that I couldn’t complete problems fast enough to balance out the workload between me and my partner. But I also was being way too tough on myself…(more on that in another post). Shout out to my partner Andres, for his patience and ability to sense that I was not enjoying this struggle with algorithms and methods. Throughout the week, he walked me through problems, asking me questions and allowing me to solve it even though he knew how to get the answer.

Which does WHAT?!? 🤔😩

One sleepless night at 2 am that project week, I was in bed googling about Ruby methods from my phone in the dark. After reading a few articles, I stumbled across someone’s medium post that explained the comparison, which finally made it click for me. They used the show “How to Get Away with Murder” as a way to explain these Ruby iterators, and it came with code snippets and memes! I also loved HTGAWM, and miss that show. (Credit to this person and thankful for their post! 👉🏻 check out it here!) I’m a visual learner, and just reading new material won’t always do the trick, especially trying to ingrain logic. I needed visuals and real-life scenarios.

Anyway, now back to our regularly scheduled programming. Now that I’ve understood the differences of these iterators, here is my take of explaining the differences between the Ruby enumerable power-players, in hopes that it helps others visualize which method to use best when iterating over arrays, hashes, and ranges.

*NOTE* As of me writing this post, the preseason games just finished last week. We will take a look at the Philadelphia 76ers vs Boston Celtics first preseason game, and only the starters from that game.

Let’s model our data into classes. We will be using these player stats for each of our examples below.

.each

Based on the data above, we just want a list of all the player’s names on both teams. We will use .each to accomplish this.

  • The .each iterator will return all the elements of an array or hash.
  • Each always returns the original, unchanged object.
  • The code in the block (between doend or { } the short way ), performs an operation on each element to get our desired result.
  • To return our array, we saved the block to a variable called “names” and return it after the block.
  • We used the .starters_names method we created above, and it returns an array of the starter’s names.

Perfect — let’s get this game started!

.select

Wow, so many fouls already?!? These two teams are, after all, one of the biggest rivals in the NBA. Let’s get a list of the players that have four or more fouls so we can make adjustments to our lineup. We will use .select to perform this method.

  • .select goes through the entire array of our player’s stats and will take the elements for which we specified in our block as true.
  • In this case, we want the stat (in our block) to get the personal fouls of each player (pf for “personal fouls”) that are greater than or equal to 4.
  • Since we want a list of players that have this value, we save this block to a variable again, this time called “trouble_makers”, and then return this variable at the end to get our list of players.

Coach Brad Stevens has some thinking to do about this lineup.

What about turnovers….let’s see who’s giving the ball away way too many times. We will use .select to do this as well.

It’s basically the same exact code as above, but we want .tov for turnovers.

Okay, I’m just having fun with the memes at this point because we can’t judge everything off preseason games. But Coach Doc Rivers is not used to all these turnovers, so let’s see his facial expression upon seeing the high TOs.

.find

Let’s see who is the tallest player on the court amongst all the players are, but we just want the first guy that is taller than 6ft. 8in. We use .find to perform this method.

  • .find will go through an array and return only the FIRST element, in which the condition in the block is true.

I guess lucky for Joel Embiid that he was the first on that list. But he’d like us to think he’s the only tallest one of the group, naturally.

.map

Let’s say the digital team wants all the player's names for each game to be all caps for banners, marketing, and other stuff for the jumbotron. They don’t want to have to manually type the names in caps for each game. That’d be tedious. Let’s use .map to achieve this!

  • .map will return the array or hash with the same number of elements as the original, however, the new array will be modified.
  • .map is mainly used to transform data.
  • .map won’t change the original, which is great, because we only want the uppercase version for banners at the stadium and still need our original.

And there we have it, Ruby’s starting team!

If you are about to begin your coding journey and happen to be learning Ruby, I highly suggest getting intimate with .each, .map, .select, and .find. It will save you a bit more time in the beginning. In fact, if there is anything to memorize, these four are the All-Stars of the bunch.

Good luck on your learning adventure and TRUST THE PROCESS!

P.S. If you couldn’t already tell, I’m a Sixers fan. So apologies in advance for any Celtics fans I may have offended. Good Luck…but go Sixers! 😬

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Mary Ann Navarrete

Written by

Hospitality Sales & Marketing turned Software Engineer. I love building things, solving problems and analyzing complex data. Follow my journey.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store