Array Enumerable Methods in Ruby
Holly’s Code Corner — Episode 2
In this blog, I am going to introduce 7 array enumerable methods in ruby.
Introduction
Welcome to episode 2 of Holly’s Coding Corner. Today, we will be talking about array enumerable methods in Ruby.
These methods work by iterating over each individual element in the array, and performing some operation on the element.
Below, I will go through each method and describe what they do, and why you would want to use them.
#1 each
The each method iterates through the array, and performs the operations you define before the end block. This is similar to a forEach block in Javascript.
This method does not have any return value.
[1,2,3,4].each do |num|
puts "I am now iterating through #{num}"
end
==> I am now iterating through 1
I am now iterating through 2
I am now iterating through 3
I am now iterating through 4#2 each_with_index
This method is very similar to the each method above. The only difference is that inside the .each block, an additional index variable is available, which tells you the index of the element that is currently being processed.
This method does not have any return value.
[1,2,3,4].each do |num,index|
puts "I am now iterating through #{num},its index is #{index}"
end
==> I am now iterating through 1,its index is 0
I am now iterating through 2,its index is 1
I am now iterating through 3,its index is 2
I am now iterating through 4,its index is 3#3 collect / map
The collect and map functions take each element, perform some operation on them, and then place the resulting new element into a new array. For example, if you have an array with numbers in it, you can use map or collect to get a new array with each number multiplied by 2.
Get Holly Ye’s stories in your inbox
Join Medium for free to get updates from this writer.
These two methods return a new array.
[1,2,3,4].collect do |num|
num * 2
end
==> returns [2,4,6,8][1,2,3,4].map do |num|
puts num * 2
end
==>returns [2,4,6,8]#4 filter / select
Filter and select method return all the matches from the array. This can be used to select only the elements from the array that match a certain condition, such as only the even or odd numbers in an array.
[1,2,3,4].select do |num|
num.even?
end
==> returns [2,4][1,2,3,4].filter do |num|
num.even?
end
==> returns [2,4]["hello", nil, "world"].filter do |word|
if word != nil
word.upcase
end
end
==> returns ["hello","world"]
* The reason that the above code does not return ["HELLO","WORLD"] is that
filter and select method can only return elements from the original array.
In order to return ["Hello","World"], you need to filter first then map
over the filtered array.#5 .find & find_all
Find method returns the first match from the array, and find_all returns all the matches from the array. This can be used if you want to know the first element in an array that matches a condition, or all elements that match a condition.
[1,2,3,4].find do |num|
num.even?
end
==> returns 2
* .find returns the first match[1,2,3,4].find_all do |num|
num.even?
end
==> returns [2,4]
* .find_all returns all the matches#6 .sort
Sort method sorts an array, and returns the sorted array.
[1,2,12,5].sort
==> returns [1,2,5,12]["Holly","Angela","Brandon","Darrian"].sort
==> returns ["Angela","Brandon","Darrian","Holly"]#7 tally
This method iterates through the array, counts the occurrences of each element and returns a hash with the elements of the collection as keys and the corresponding counts as values.
[1,2,2,3,4].tally
==> returns
{"1"=>1,"2"=>2,"3"=>1,"4"=>1]I hope you find this blog helpful. Thank you! 🌻









