Which Ruby Enumerable Should You Memorize?

Lee Michaeli
5 min readOct 19, 2020

Well, let’s start off with telling you that you should memorize a few. Specifically .each, .map, .select, and .find.

Now before you leave, thinking you were bamboozled into needing to only memorize one magic enumerable that can solve all your problems, I promise you it won’t be that hard. Or long!

Although I myself am only 2 weeks in on my coding journey, I have already found the immense value of learning, understanding, and memorizing the functionality of each of these in order to further my learning and enjoyment of coding. Rather than make coding a job or work, it can be fun and challenging.

Now before I start the snooze fest of how important learning is, let’s just jump into it and have the cast of How To Get Away with Murder help me. Well mostly Viola Davis, but the students can come along for the ride.

First let’s start with our model. We’ll be looking at a university class with our professor Annalise Keating.

.each

Annalise, needs every student to get a message, but make it a little personal and include their name. You know, make them feel special so they don’t want to drop the class yet.

First let’s make the class. We’ll have five students.

Then we write our method to send them each a message!

Here we look at the array of all Student instances and we use .each to take each instances name. And for each instance, we make a personal message with their name!

Now they’ll be fooled!

.map

Now with .map, not only can we return objects, but we can change them! On top of this, .map take the liberty of creating a new array for you. You might wonder why you would want that. Often times, you won’t want to change the original array even if you need to transform it in some way for a method. This way you can keep the original without any messiness.

Now let’s say Annalise wants to keep a list of everyone's name in full caps in case she’s exceptionally angry with them.

Just for fun, we’re also going to let Annalise keep a list of everyone's names as normal and again store it in a new array.

Now the Annalise is keeping great track of both their normal names and their names in full caps lock for any occasion! Not altering the original array of Student instances which we got the names from.

.select

Let’s say Annalise is picking favorites from her students and she likes her students that have a name shorter than 6 letters. Why not? So we want to pick Wes and Asher.

Here we are look at each student’s name and if it’s less than 6 letters, we select them into the array and return all the Student instances that fit the criteria.

Nice! And if we didn’t want the whole instance, but just their lovely less than 6 letter names, we can use .map like before!

And just like that Asher (and Wes) made it onto Annalise’s good side.

.find

There’s often confusion with .select and .find but the key difference is that .find doesn’t return all instances that fit the criteria, but only the first one that appears. So if Annalise wasn’t in the mood to deal with multiple students, she’d just use .find in order to find the first Student instance that matches her criteria of having a name with less than 6 letters

With just that simple change of .select to .find we now only get…

Nice! You made it all the way through!! And survived all the better. Now rather than having one magic enumerable to cure all. You’ve learned 4 that will help you go a long way.

And if you find yourself in a niche situation where these don’t cut it, don’t forget your friend Google, where you can add even more to your mental library. (Though you should be okay with just these 4 to start off)

Although with coding, at first you want to be able to reach the goal as fast and easy as possible, the main thing to do starting off is to understand. It’s age-old saying, but practice really does make perfect. Making different models, and experimenting is a great way to learn. It can even be fun! Shocking, I know.

Now go off with your new knowledge and code!

--

--

Lee Michaeli

Recent college graduate and just starting to learn to code. Try my best to learn and have fun along the way :)