Intro to Enumerables
Tags: ruby, beginners, codenewbie
Published: 2020-02-09 20:38:59

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](https://dev-to-uploads.s3.amazonaws.com/i/fpwwxr73yie4b627eumj.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/giuk88fqvz133esvtead.png)

* .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](https://dev-to-uploads.s3.amazonaws.com/i/6ujser67rhs0zz0v0u8g.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8vtfhuf6owr2qqzbozk6.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/i/ayyc8mzw7m4rrsw53ylb.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6y80xk6zf7ngwysxnokm.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/i/0e0bmx82goc5ue573o6p.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6y80xk6zf7ngwysxnokm.png)


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](https://dev-to-uploads.s3.amazonaws.com/i/kg3eo4egmhy7kou0p0zx.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xtv1g77s2fn0h9jd2cnt.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nbifor0nyo3fjonhf8cd.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hoyne5cp7m0bu8f4be7u.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4fuf81k90isl3dtbme1y.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9gkvo8d9rj3twezso66d.png)


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](https://dev-to-uploads.s3.amazonaws.com/i/xm31wsaykqgymf8ry57w.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/atl7ehyg0pf8il3ruqxd.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/4ni8bkzhy0tldbs4h2ni.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ssu06o2728yb9759x50c.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/17swej15ehzpqyovquye.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hzhr5j43i91g5m1wasf3.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zwnf44sf6snm07ogi99j.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zx8ytvmvk2vspfosq6dc.png)

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