Ruby Enumerable methods? What are they?

The following is going to talk about what an enumerable is in Ruby ,what some basic Enumerable methods can do for you when applying them to a collection of data and what an enumeration is.

Real quick it might just be me but this word was kind of hard to say when I first encountered it. So here is how to pronounce it: e·nu·mer·a·ble.

To help you understand what a Ruby Enumerable is and what it does. First here is the definition of enumeration:

The action of mentioning a number of things one by one.

Now in Ruby, we can call an Enumerable method on a collection of data, pass in some sort of argument to the method which is know as “the block” and get back a different collection of data based on what that a based on what was in the block.

There are lots and lots of very helpful Enumerable methods. So much so that if I were to talk about them all in this article it would be extremely long. So I am just gonna talk about a few basic ones that are a great starting point for a beginner in Ruby. #Select, #Map and #Find

#select

Returns a new array containing all elements of ary for which the given block returns a true value.

We can call the #select method on a collection of data and it iterates through each element in that collection of data and returns a new collection with all of the elements that came true against the block. Here is an example:

#map

Invokes the given block once for each element of self.

Creates a new array containing the values returned by the block.

For the #map method(which is also the same thing as #collect)when it is called on a collection of data it preforms whatever is in the block to each element and returns that collection of data modified . Here is an example:

#find

Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil otherwise.

For the #find method when it is called on a collection of data it iterates over each element and returns the first element that makes the block true. Here is an example: