Map, Select, Find and Each

Ben Dunjay
5 min readApr 14, 2020

--

I’m trying to write these blogs for someone maybe just starting out at the very beginning of coding who may be finding the concept of hashes very confusing. So as my previous blog mentioned an Enumerable will automatically iterate through the array or hash it is passed until it has completed the task. Below I have listed the four Enumerable's that helped me get off the ground and start understanding how to extrapolate the data I was looking for from arrays and hashes. Writing my own basic arrays and hashes helped me to understand the structures of both the array and hash, which then helped me use the Enumerable’s the way I wanted. There is a vast amount of Enumerable’s out there, all helping coders solve their problems. I have focused in on the first 4 but here is the link should you wish to browse more. The likelihood is, if you’re thinking I want to do this to the code, then there is probably an Enumerable to help you get there!

https://ruby-doc.org/core-2.7.1/Enumerable.html

So let’s start simple:

As mentioned in https://docs.ruby-lang.org/en/2.0.0/Array.html arrays are ‘ordered, integer-indexed collections of any object.’ So in an even simpler term, an ordered list of elements with an index starting at 0 for the first element in the array.

A super simple array

‘A Hash is a dictionary-like collection of unique keys and their values’ (https://docs.ruby-lang.org/en/2.0.0/Hash.html). A hash could be seen as a way to group a collection of information that can refer to the key. See below for an example.

A super simple hash

This is where the Enumerable come into their own. They can make simple arrays and hashes look even more simple and complicated arrays are slowly broken down.

Map.

This will iterate through and create a new array. So by using our array_name we can say array_name.map and return a single array with all the names in.

Not much has changed! Well this is true for now, but when you come to more complicated code you will thank the heavens you understood the simple stuff. Because when code becomes too complicated it’s difficult to follow, so you should always be looking to break it down into manageable chunks with each part of code helping you solve a specific issue.

This can prove useful in mapping over hashes, of which you will come across many in your coding life. Say we wanted to return an average of all the grades, a good way to start would be to create an array with just the scores in!

We can use map to create an array with the score of each person. But wait, how did I get just the score. If you look above I have written out the map twice. Line 5 shows that the Enumerable looks at two points within the hash. First it looks at the key, then at the value of the key. I have then asked for the value to be returned. On line 6 I have written it out the exact same, but simply changed key and value to be more descriptive of what exactly I am calling. This will help coders and yourself in the future understand what you were trying to code. For instance, I may be looking to create a method that returns the average score of the whole class.

Select.

This will return all instances of what you are looking for in the array or hash.

Here we have been able to select any names beginning with B, and now for the hash!

Now this is something select will do. Return the whole instance from a hash that meet the criteria, which means you can break the data down further if you wish. This would allow me to see all of their data. So if each person had a hash of their subject and score, I would be able to see that and get an idea of how they’re performing or maybe how they’re improving!

Find.

Here we have find. But wait… I asked the Enumerable to look through the array and find word length equal to 3… why didn’t it return Sam too? Find will look through the array and stop when it finds the FIRST element in the array that meets the criteria. It won’t look any further. If you wanted to find all the names with a length of 3, which Enumerable mentioned do you think would be better?

You guessed it. It does the same with the hash, but notice how it has returned it in an array so be careful how you use this data! If I put the answer into a variable and called variable_name[0] I would only get James returned back to me, not his score as well.

Each.

Be careful. Each returns the array object it was called on and the result of the Enumerable is discarded. Hence the console simply receiving an array back. However, because we used puts, the Enumerable will display that message on screen for each name in the array until it has finished. So be warned, if you had 1000 names, maybe each isn’t the best Enumerable to use if you are just looking for one name. Because even if Each finds it. It will carry on through the WHOLE array until it has iterated the code over each element.

Remember, each will discard the result of the Enumerable. We caught these three cheating! So we want to display to them their new score, this will iterate over all three scores and again, because we used puts it will output to the screen their new score whilst returning to the console the original hash.

So there we have a basic overview of 4 very handy Enumerable's, play around with them. The more you can break your code down to a simple level that you and others would understand helps make you a better coder! Remember to think about what you are trying to achieve before you start coding. Which enumerable would best help you reach your answer. You can always take the data you found from the first enumerable put it in a variable and then use another enumerable to refine the data even further, you don’t have to try complete it all in one line of code!

--

--