Enumerable is a mixin that is used to add additional functionality
to collection classes like Array
and Hash
. This means that you can use the
methods defined in Enumerable
on both arrays and hashes.
Enumberable#map is useful when you want to create a new array by iterating over some collection of values, and calculate a new value based on each item in the collection.
We might want to create a new array by iterating over an array of integers, adding 100 to each value.
One way to do this without using map is to use #each
:
numbers = [1, 2, 3, 4]
bigger_numbers = []
numbers.each do |number|
bigger_numbers << number + 100
end
bigger_numbers
# => [101, 102, 103, 104]
This can be simplified by using #map:
numbers = [1, 2, 3, 4]
bigger_numbers = numbers.map do |number|
number + 100
end
# => [101, 102, 103, 104]
A new array will be created using the result of each iteration through the numbers array.
Enumerable#sort is great when you want to sort an array of strings alphabetically:
people = ['Eric', 'Adam', 'Richard', 'Helen']
sorted_people = people.sort
# => ["Adam", "Eric", "Helen", "Richard"]
Or sort an array of integers by their value:
numbers = [1, 15, 2, 4, 17]
sorted_numbers = numbers.sort
# => [1, 2, 4, 15, 17]
Enumerable#sort_by is great when you want to sort by a more complicated value.
Here's an example where we sort an array of hashes where each hash represents a person. We can use #sort_by to sort the array by each person's last name:
people = [
{ first_name: 'Eric', last_name: 'Kelly' },
{ first_name: 'Adam', last_name: 'Sheehan' },
{ first_name: 'Richard', last_name: 'Davis' },
{ first_name: 'Helen', last_name: 'Hood' }
]
sorted_people = people.sort_by do |person|
person[:last_name]
end
# => [
# {:first_name=>"Richard", :last_name=>"Davis"},
# {:first_name=>"Helen", :last_name=>"Hood"},
# {:first_name=>"Eric", :last_name=>"Kelly"},
# {:first_name=>"Adam", :last_name=>"Sheehan"}
# ]
Enumerable#find is useful for searching for specific items within a collection.
Imagine we have an array of hashes, each representing a person. We can find the person with a given first name using #find:
people = [
{ first_name: 'Eric', last_name: 'Kelly' },
{ first_name: 'Adam', last_name: 'Sheehan' },
{ first_name: 'Richard', last_name: 'Davis' },
{ first_name: 'Helen', last_name: 'Hood' }
]
richard = people.find do |person|
person[:first_name] == 'Richard'
end
# => {:first_name=>"Richard", :last_name=>"Davis"}
In the previous example, the #find method will iterate over each person
hash until it finds one where the first name is 'Richard'
.
Enumerable#find_all, like #find, is useful for searching for specific items within a collection. The difference is that #find is only going to return the first item that matches the given conditions. #find_all is going to return an array containing all of the matching items in the collection.
For example, we can find all of the people with the first name Richard:
people = [
{ first_name: 'Eric', last_name: 'Kelly' },
{ first_name: 'Adam', last_name: 'Sheehan' },
{ first_name: 'Richard', last_name: 'Davis' },
{ first_name: 'Helen', last_name: 'Hood' },
{ first_name: 'Richard', last_name: 'Simmons' }
]
all_the_richards = people.find_all do |person|
person[:first_name] == 'Richard'
end
# => [
# {:first_name=>"Richard", :last_name=>"Davis"},
# {:first_name=>"Richard", :last_name=>"Simmons"}
# ]