Iterator methods in Ruby

Seth Blanchard
3 min readNov 22, 2020

In Ruby, a programmer oftentimes needs to go through a collection, either array or hash usually, and operate on each element of this collection in some way. Using methods in the iterator category are a great way to do this. There are some fundamental similarities between the types of iterators in that they all operate on a collection. They all function similarly in loop form and will complete a block of code once for every object in this collection. For each step, they will use the current element as an argument to be assigned to a block variable. They will then use that assigned variable to execute the block of code. Each iterator block functionally begins with “do” and finishes with “end.” However, each type of iterator has different functionality and can be utilized for distinct tasks. Some of the most common iterator methods are .each and .collect/.map.

The .each method in Ruby is probably the most commonly used iterator method. Like the name implies, .each will iterate through a collection and execute the block of code in the manner of all iterator methods. One important thing to remember with .each is the return value. While .each can output based on the specific code block called, the return value will always be the original collection. You are not altering your collection. One can find myriad uses including puts statements with interpolations of each element in the receiver array/hash. Chaining the method .with_index is commonly used to iterate through a collection and create a numbered list.

The .map method, also known as .collect is an abstraction of the .each method and provides a different functionality. Both .map and .each will iterate through all elements in a collection but, while .each returns the original collection, .map is used to return a new and altered array based on the code in the given block. .Map will not change the original collection but returns a new one. If one were so inclined, one could use .map! and this would overwrite the receiver collection. The main functionality for .map is to create a new collection of objects which can then be operated on throughout the rest of your code.

During my first program evaluation (CLI program) I was asked to alphabetize a numbered list I had created using .each.with_index. Having done this quite a few times in various course labs, I was surprised to have drawn an almost complete blank when asked to recreate this process. I can hopefully put this lapse down to the pressure of coding in front of someone. Practice will undoubtedly help me with live coding in the future. With a little assistance from the instructor, I was able to locate and utilize the .sort_by method. .Sort_by is a great method to use when you want to sort a collection in a certain way. One can sort by string length, the contents of the string, or whether numbers are even/odd.

In this case, I was asked to sort alphabetically. I tripped myself up again at attempting to use .sort_by on all of the objects in a class. This obviously wasn’t going to work because I was really only interested in alphabetizing the name attribute and .sort_by hadn’t been told what to sort within the collection of objects. Once I determined what I was actually attempting to operate on, I was able to correctly chain my .sort_by method to my collection of data. In this instance, I sorted by the name attribute and produced an alphabetized list of general locations in Virginia which the user could then choose from.

--

--

Seth Blanchard
0 Followers

Full-Stack Software Developer- Founder of Skylark Hammocks, BEST, LLC, and several other as of yet unrealized ventures.