Loops and Iterating Over Data Containers in Ruby

Sam Ferebee
5 min readFeb 16, 2021

Loops are a fundamental part of computer programming in every programming language. Whether you’ve worked in C++, Java, C#, or JavaScript you’ve come across and utilized one or many of these different types of loops: while loops, do while loops, for loops, for-each loops, range-based for loops and until loops. While each language has its own syntax and flavor of how to write loops, loops are fundamental to every language and function similarly across the computer programming ecosystem.

Loops are used, surprisingly, when you want to execute an action repeatedly, or “loop” an action over and over again. In Tetris the game loops continually until the blocks have reached the top of your screen and you lose. In Microsoft Word the application loops perpetually until you quit the application or turn off your computer. As a matter of fact, your operating system that you are using to view this blog, whether it be Windows, OSX, or Linux is looping over and over to stay running until you turn your machine off! Loops are everywhere and they are very important to understand.

Loops and conditionals go hand in hand. In the below examples the loops are perpetual until a specific event or action triggers their ending: until a condition is met. For example, if a game is set to loop until a player has died, the loop can look as simple as this:

simple while loop

This loop will run forever until the player runs out of lives.This same loop can be written differently, but you will see how it is still governed by a condition, and will still achieve the same thing:

this will loop continually as long as the player is alive, and stop running when the player dies because the condition the loop is running on (player_is_alive) will be set to false

We can write this same simple loop yet another way:

making use of the #loop and #break keywords

Here we see the #loop and #break keywords in action. #Loop followed by a do end block will loop forever. However, we are able to break this loop when a condition (player_is_dead) is met using the #break keyword which simply breaks out the loop it is within. We can see it in action in the following simple code:

The above #loop loop can be rewritten to achieve the same exact thing in a #while loop:

On the flipside of a #while loop Ruby has #until loops:

Another form of loop is the “do while” loop which is written to ensure that the loop executes at least once, then the condition to continue the loop is checked. This form of a do while loop is not all too common in Ruby but it exists nonetheless:

this will execute the action in the loop while the end while line is true (in this case, keep printing while i is less than 4)

All of the above loops are examples of looping an action over and over again, a very useful thing to do in many situations. Additionally, loops are extremely useful when a programmer needs to iterate over or loop over a given data container to access each element of the container until the end of the container is reached. For example, you could iterate over an array of integers using a while loop as follows:

While this code certainly works, it can be written better in Ruby, we aren’t writing C++ here! Ruby has easier access to iterating over a data container:

iterating over an array using a for loop

This is much more like the Ruby we know and love. We don’t need to create a separate counter variable, use that variable to access each element of the array, and have the while loop run conditionally until the counter equals the size of the array. Ruby takes care of all of that using a #for loop.

Ruby makes looping over data containers incredibly simple and has a variety of built in methods to loop over a container depending on what it is you want to achieve. These set of built in methods are called enumerables. While the above loop is completely acceptable it can also be written using the simplest Ruby enumerable, the #each keyword.

Here we use the #each keyword to iterate over (visit each element within the array) the array and print out each number of the array. How easy is that? Ruby has many built in enumerables that make programming in Ruby smooth and easy. One such enumerable is #sum:

this will print out 6 thanks to the ruby #sum enumerable

We can use the #select enumerable to pull out elements of a data container that meet whatever our specified condition is:

outputs:

There are many built in Ruby enumerables. Some common ones include #find, #find_all, #map, #any?, #all?, #min, #max, and #sort. A full list of Ruby enumerables can be found here: https://ruby-doc.org/core-3.0.0/Enumerable.html

Hopefully this post can help you understand the importance of loops, their use cases, and a variety of ways they can be written. Moreover, I hope it shows you how powerful yet simple Ruby enumerables are, they make looping over data containers simple, clean, and elegant. Such built in methods don’t exist in every programming language, so if you are a Ruby programmer make sure to take advantage of these amazing built in methods!

--

--