Ruby Array Enumerable Cheatsheet

Jake Berman
Sep 6, 2018 · 4 min read

Enumerables allow programmers to iterate and operate over Ruby Objects and Arrays. Ruby has tons of useful built-in Enumerables, however, when I first started using Enumerables I found reading documentation online daunting so once I learned how to use the .each Enumerable I would just figure out how to make the .each Enumerable work for the problem I had at hand using additional variables and if/else statements. In this write-up I hope to provide a quick easy to read write up of some other useful Enumerables.


.Each

Iterates through each item in an array and executes code in enumerable code block (in between the brackets, or do and end) and will return the original array.

arr = [1, 2, 3]arr.each_with_index do |item|
puts item + 1
end
Will output into console:
2
3
4
Will Return:
1
2
3

.Each_with_index

The Each with Index Enumerable is very similar to the each enumerable with the added benefit of also keeping track of the current index of the array as it goes through.

When to use: Each with index is useful for outputting numbered lists or any time you need to iterate through an array while also keeping track of the index.

arr = ["pen", "paper", "pencil"]arr.each_with_index do |item, index|
puts "#{index}. #{item}"
end
# =>
1. pen
2. paper
3. pencil

.Find

The find enumerator goes through the given array in order. It tests each item in the array against a given condition, and it will return the first item for which the condition returns true. If none of the items meet the condition will return false.

How to use:

array.find do |item|
condition which evaluates as true or false
end

When to use: Whenever you are looking for the first item in an array which meets a certain criteria

arr = [1,2,3,4,5]array.find do |item|
item >= 3
end
# => 3

.Select

The select enumerator goes through the given array in order. It tests each item in the array against a given condition, and it will return an array of all the items for which the condition returns true.

How to use:

array.select do |item|
condition which evaluates as true or false
end

When to use: Whenever you are looking for the all the items in an array which meets a certain criteria

arr = [1,2,3,4,5]array.select do |item|
item >= 3
end
# => [3,4,5]

.all?

Goes through an array and tests each item for a condition. If every item in the array returns true for condition the enumerable will return true.

How to use:

array.all? do |item|
condition which evaluates as true or false
end

When to use: When you want to test an entire collection of items for a condition

arr1 = ["dog", "dog", "dog"]arr2 = ["cat", "dog", "dog"]arr1.all? do |item|
item == "dog"
end
#=> Truearr2.all? do |item|
item == "dog"
end
#=> false

.Any?

Goes through an array and tests each item for a condition. If any item in the array returns true for condition the enumerable will return true.

How to use:

array.any? do |item|
condition which evaluates as true or false
end

When to use: When you want to test an entire collection of items for a condition

arr1 = ["cat", "cat", "cat"]arr2 = ["cat", "dog", "dog"]arr1.any? do |item|
item == "dog"
end
#=> Falsearr2.any? do |item|
item == "dog"
end
#=> True

.Collect/.Map (Collect and map both do the same exact thing)

The Collect enumerable goes through each item in an array and will create a new array of the same size filled with whatever is returned at the end of enumerable code block

How to use:

array.collect do |item| 
#additional code can be placed between enumerable and final
#return value
return value of block
end

When to Use: Collect is useful when you want to iterate through/operate on the the items in an array but leave the original array unchanged

arr1 = [1,2,3]arr2 = arr1.collect do |item|
item *= 2
end
arr1 #=> [1,2,3]
arr2 #=> [2,4,6]

Note: If every iteration of the collect enumerable does not have a return statement does not return something, the collect array will be filled with a nil statement in its place. The .compact enumerable can remove the nil statements from the array.

arr1 = [1,2,3,4,5,6]arr2 = arr1.collect do |item|
if item >= 3
item
end
end
arr2 #=> [nil,nil,3,4,5,6]
arr2.compact #=> [3,4,5,6]

.flat_map

Operates similarly to the .map/ .collect enumerator; however it can be used on nested arrays. Flat map will go through the array from left to right as it appears in your console/text-editor .

2Darray.flat_map do |item| 
#additional code can be placed between enumerable and final
#return value
return value
end

When to Use: When you want to convert a nested 2-dimensional array into a 1-D array.

array = [1,[2,3,4,5],[6, 7],8].flattened = array.flat_map do |item| 
item
end
flattened #=> [1, 2, 3, 4, 5, 6, 7, 8]

.Max_by

Iterates through the array items and returns the object which returns the highest return value from the block.

array.collect do |item| 
#additional code can be placed between enumerable and final
#return value
return value of block
end

When to use: Useful for when you are trying to maximize by something rather than just integer value.

array_of_stings = ["a string", "a very very long string", "very long string"]array_of_stings.max_by do |string|
string.length
end
# => "a very very long string"

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade