DEV Community

EidorianAvi
EidorianAvi

Posted on

Enumeration in Ruby

Enumerables in Ruby

Today we're going to be discussing enumeration in ruby and also three of the most basic but most effective enumerables ruby has to offer for arrays. First let's start off with: What is an enumerable in ruby?

The act of enumeration is to go through a collection one by one. The Enumerable Module is a package of many methods that can be performed on collections of data such as arrays or hashes. These methods in turn are used for searching, sorting, traversing and general collection manipulation. To reiterate they can be used to order the data in a desired manner, to change every piece in the collection, or to grab only the pertinent pieces of data. Certain ruby collection classes such as Arrays or Hashes already come packaged up with this enumerable module and thus have access to its sizeable collection of enumerating tools(methods) or wait for it..
enumerables.

For each example of enumerable usage I will be providing two forms of the same line of code. The first will be fully typed out for clarity and the second will be a one liner that is the same code but is generally favored in the ruby community IF the code block is able to fit in one line like so:

Alt Text

Let's talk about .each

If you break an enumerable down to its most basic of building blocks it's a looping method built for you. The most basic iteration of these looping methods is the .each enumerable that will go over every single item in the collection and perform whatever block of code you set it to but will also return the array unaltered.

Alt Text

Now this is useful of course and does perform the iteration of your design which is powerful in itself but lacks in multiple ways from a developers standpoint. The biggest downfall is you will have to be very specific about what you're trying to do with your enumeration when in reality there is likely another enumerable already designed to do that for you. The second is it lacks in semantic clarity and doesn't really convey what you're trying to do with it outside of the code block you or another developer will have to decipher in future usage. It gets the job done but if you have another more fitting tool in the toolbox it makes sense to use that right? Right.

The Three Essentials

Here are three of the most common and useful enumerables any rubyist can have in their arsenal as well as when it's effective to use them.

1. Map (Collect)

The .map (or .collect) enumerable is perfect for going over every item in the collection and transforming them by means of your code block. Not only does it transform the collection but it returns the collection in it's newly transformed state.

Alt Text

One key thing to note is that when you use a map it will always return a collection of the same size just transformed. Perfect for manipulating every piece of data without having to go through one by one.

2. Select

The .select enumerable is designed to find only the items in the collection that meet whatever code you set in your code block. It returns a new transformed version of what it was performed.

Alt Text

It will grab ALL data that matches the criteria meaning the new array can be either the exact same length as the original array or shorter depending on the code block. Select is perfect for grabbing only what you're looking for in the collection whether it be all of it or just a few items.

3. Find

The .find enumerable is very similar to select but differs in one crucial way. It returns only the first element in an array that matches the condition set in the code block.

Alt Text

Find is useful in the case that you know only one element should match the condition or if you're trying to distinguish if anything matches the condition in the first place.

Just a Taste

That'll be it from me on enumerables for today but hopefully this gave you a taste of the power of enumerables in ruby. Here's a link to showcase all the different enumerating goodness you get in ruby:

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

Top comments (0)