Getting to know my Enumerables

Nicholas
3 min readSep 9, 2020

--

When starting off getting to learn ruby, one of the most useful tools that can be put in your tool belt is ENUMERABLES. They are a useful way to sort through arrays and hashes, and manipulate the data however you would please. At the start of my journey of getting to know ruby i find myself leaning toward a handful of enumerables which are each, map, select, find, and reduce.

When first starting off you are introduced to the .each method. This is a perfect tool for simply looping through an array and having the changes that are wanted returned.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]array.each do |num|puts num * 5end==> returns [5, 10, 15, 20, 25, 30, 35, 40, 50]array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

As shown above the each method works fine with returning what it is told to return, however it does not save any of the data it returns, so the original array is still the same it was in the beginning. In order to save the changes that were made, more code would have to be written for saving the elements into a new array. In order to save time there are three enumerables that can do the job without having to manually save the changes that you wanted.

The map method does exactly the same thing the each method does except there are no need for the extra steps to save the elements into a new array, it automatically does this.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]array.each do |num|puts num * 5end==> returns [5, 10, 15, 20, 25, 30, 35, 40, 50]new.array = [5, 10, 15, 20, 25, 30, 35, 40, 50]

As shown this method is quicker at changing an array and recording those changes than the each method. The map method is often compared to the collect method because of there similarities.

Select is a method that are best known to work with true or false statements aka boolean values. The select method are used in order to “select” the desired elements, which are based on the the criteria that was set in the true or false statement.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]array.select {|num| num < 7}==> [1, 2, 3, 4, 5, 6]

In the example shown above, the statement was to find the numbers in the array that where less that 7, as you can see the array only returns the values where that statement is true, and has removed all of the elements that do not fit this criteria. The select method is also found to work fairly similar to the find_all method

Now we have when through all the tools I use so far the map method goes through an entire array and changes it, the select method picks only specified elements, and now we are at find which is not something that I use a lot but can be useful if you you are only trying to find one element. The find method will return the first method that fits the criteria that has been set.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]array.find {|num| num.even?}==>[2]

For this example it was looking for even numbers, but even though there were other even numbers in the array only the number 2 was returned because it was the first one.

The final enumerable that is useful, but I do not have much experience with is reduce. Reduce is an enumerable that only returns a single element/item, wether it is the sum of an array or the shortest string in an array.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

array.reduce(:+)
==> 55

As seen in the example the array took the entire array and added it together. This method is often compared to the inject enumerable.

--

--

No responses yet