Iterate Through a Ruby Array

Nurudeen Ibrahim Jan 03, 2022
  1. Iterate Through a Ruby Array Using the each Method
  2. Iterate Through a Ruby Array Using the for Loop
  3. Iterate Through a Ruby Array Using the reverse_each Method
  4. Iterate Through a Ruby Array Using the each_with_index Method
  5. Iterate Through a Ruby Array Using the map Method
  6. Iterate Through a Ruby Array Using the select Method
  7. Iterate Through a Ruby Array Using the reject Method
Iterate Through a Ruby Array

The array class in Ruby includes the Enumerable mixin, and as a result, it has several transversal methods. In this tutorial, we will be looking at these enumerable methods and other ways of iterating through an array in Ruby.

Iterate Through a Ruby Array Using the each Method

This is the most used iterator in Ruby.

Example Codes:

fruits = ['Orange', 'Apple', 'Banana']

fruits.each do |fruit|
  puts fruit
end

Output:

Orange
Apple
Banana

Iterate Through a Ruby Array Using the for Loop

This method can have an undesirable side-effect, and as a result, it’s not recommended.

Example Codes:

fruits = ['Orange', 'Apple', 'Banana']

for fruit in fruits
  puts fruit
end

Output:

Orange
Apple
Banana

If a fruit variable had earlier been defined in the example above, the for loop would overwrite it with the last element of the fruits array. It is a scoping issue, and the behavior is illustrated below.

Example Codes:

fruit = 'Mango'
fruits = ['Orange', 'Apple', 'Banana']

for fruit in fruits
  puts fruit
end

puts fruit

Output:

Orange
Apple
Banana
Banana

Iterate Through a Ruby Array Using the reverse_each Method

As the name implies, this works like the each method but in reverse order.

Example Codes:

fruits = ['Orange', 'Apple', 'Banana']

fruits.reverse_each do |fruit|
  puts fruit
end

Output:

Banana
Apple
Orange

Iterate Through a Ruby Array Using the each_with_index Method

This method is useful in a situation where you need to get each element of an array and the index.

Example Codes:

fruits = ['Orange', 'Apple', 'Banana']
fruits.each_with_index do |fruit|
  puts "#{index}. #{fruit}"
end

Output:

0. Orange
1. Apple
2. Banana

Although the above examples demonstrate the simplest way of iterating through an array, the methods usually come in handy when you need to traverse through an array and carry out some operations on each of its elements - for example, iterating through a list of email addresses and sending a message to each of them.

The remaining methods we will mention in this tutorial are a bit different from the ones mentioned above in the sense that they transform an array into another one.

Iterate Through a Ruby Array Using the map Method

It is useful if you want to modify each element of an array and have them as a new array.

Example Codes:

numbers = [2, 4, 6]

doubles = numbers.map do |n|
  n * 2
end

puts doubles

Output:

[4, 8, 12]

Note that there is also a collect method, an alias to map, and both behave the same way.

Iterate Through a Ruby Array Using the select Method

As the name implies, the select method allows you to pick only specific elements of an array that meet a specific logical condition. It also produces a new array.

Example Codes:

numbers = [2, 3, 4, 5, 6, 7, 8, 9]

perfect_squares = numbers.select do |n|
  Math.sqrt(n) % 1 == 0
end

puts perfect_squares

Output:

[4, 9]

Note that there is also a find_all method which is an alias to select, and both behave the same way.

Iterate Through a Ruby Array Using the reject Method

The reject method is the opposite of select, and it’s useful when you need to reject some specific elements of an array and return the remaining ones as a new array.

Example Codes:

numbers = [-2, -1, 0, 1, 2, 3]

positive_integers = numbers.reject do |n|
  n < 1
end

puts positive_integers

Output:

[1, 2, 3]

Related Article - Ruby Array