Each and Map Iterator Methods in Ruby

Warda Ayaz
2 min readAug 17, 2021

How does an Iterator Method Work?

Iterators are nothing but methods supported by collections. Objects that store a group of data members are called collections. In Ruby, arrays and hashes can be termed collections.

Iterators return all the elements of a collection, one after the other. We will be discussing two iterators here, each and map.

collection.method do |instance_name|
perform something on each instance using "instance_name"
end

Immediately after an iterator method is called the program must open a block. A block is a bunch of code set between do/end keywords that you can pass a method to like you can pass an argument to a method.

You can see from this format that you must create a variable name to describe each instance in the collection then call additional code using that variable name. This variable name is put in between | | pipes and is created after a new block is formed.

Alternatively, a block can also be put inside of { } brackets. If you wanted to use this formatting for an iterator method, it would look like this.

collection.method { |instance_name| perform something on each instance using "instance_name"}

Each Method

The first instance method that I want to highlight is called the .each method. This method goes through each iteration and returns each value that is contained in the original collection.

colors = ["Brown", "Orange", "Green", "Blue]
colors.each do |color|
puts "Color #{color} is #{color.length} letters long."
end

As you can see in the example above, this code will simply take each of the values contained in the colors array and place them into the sentence output using basic string interpolation #{} . The final output for the code above can be seen below.

Color Brown is 5 letters long.
Color Orange is 5 letters long.
Color Green is 5 letters long.
Color Blue is 4 letters long.

It is important to note the the return value of the collection remains the exact same after you have performed this method. For example, the return value for the original collection on our example is still [“Brown”, “Orange”, “Green”, “Blue”]

Map/Collect Method

The next method that I would like to discuss is the .map method. This method can also be called .collect, which will do the same thing in Ruby.

Like the .each method outlined above, .map will go through and grab each instance in the collection, but this method actually has the ability to change the return value of the original collection after the code has been performed. Please see below an example for how this method works.

toppings = ["pickles", "onions", "pepperoni"]toppings.map do |topping|
"I love #{topping} on my pizza"
end
Return Value:["I love pickles on my pizza",
"I love onions on my pizza",
"I love pepperoni on my pizza"]

If we had used .each in this example, the method would have printed a list just like this; however, the return value from this method would still be the original array ( [“pickles”, “onions”, “pepperoni”])

--

--