DEV Community

Cover image for Intro to Enumerables
Jin Wu
Jin Wu

Posted on

Intro to Enumerables

When it comes to looping through arrays or hashes, Ruby provides the users with many different methods or options to work with. As you may see later in this blog, there are methods that are named differently but does the same thing.

1) Each

The .each method allows you to iterate over each element in that array. To do something with that array, you need to pass in a block. A block is basically the line of code that you want to execute on each element of the array. For the case below, it would be the code between the "do" and "end".

Alt Text
Alt Text

  • .each method will return the original array

If you want it to return a modified array, you would need to add a few more lines of codes such as setting a new variable equal to an empty array and then in each iteration, add the modified element into that empty array. At the very end, you have to call the variable you've made previously in order to return the array with the modified elements.

Alt Text
Alt Text

  • One thing to note is that .each is a very primitive method. There are methods that can allow you to do the same thing but in less lines of codes.

2) Map / Collect

.map allows you to do the same thing as .each but instead of returning the original array, it returns the modified array.

Alt Text
Alt Text

  • Got the same result as .each but without the additional lines to set up the empty array and adding the modified elements into the empty array.

If you don't want to .map for some reason such as not liking the word "map", you can use .collect instead. It does the exact same thing as .map but is just named differently.

Alt Text
Alt Text

3) Reduce / Inject

A simple way of looking at .inject or .reduce is that they will sum up the numbers in a given array. What it actually does is that it takes an accumulator, in this case "sum" and iterates over each element "num" and return the sum of the two. At the end, it returns the value of the final iteration.

Alt Text
Alt Text
Alt Text

  • When you call .inject or .reduce by itself, the default value is 0.

Alt Text
Alt Text
Alt Text

4) Find / Detect

When you want to find a SINGLE element in an array, .find is the method to go. The .find method will return the FIRST element that matches the argument in the block.

Alt Text
Alt Text

You can also use .detect to get the same results. Same method, different names.

5) Select

.select in Ruby is known as filter in other languages. It's similar to .find in terms of looking for elements in an array that matches the argument in the block. The difference is .select, returns ALL of the elements that matches the argument and not just the first.

Alt Text
Alt Text

6) Reject

A filter method that is the opposite of the .select method. This method returns the an array of the elements that does NOT match the arguments.

Alt Text
Alt Text

  • .reject is a nice method to know if you're too lazy to change the argument in .select

Alt Text
Alt Text

It all comes down to personal preference of how you like to get things done.

Top comments (0)