Ruby Loop

Steve Carter
4 min readSep 27, 2021

Sound a bit like Fruit Loops when you think it. But we’re talking about blocks of code that repeat until certain conditions are reached. Like detention writing things over and over and over is not only boring but also potentially error-prone. The less code you have to write, the less chance you have of introducing bugs that can cause your program to crash and burn. If you find yourself needing to repeat an action more than once in your code, you probably need loops in your life.

Loop

Ruby’s loop is an infinite loop that will keep going unless you specifically request for it to stop, using the break command. Most commonly, the break is used with a condition, as illustrated in the example below.

this loop isn't used much in Ruby. If you find yourself using loop, know that there is probably a better loop for you out there, like one of the more specific loops below.

While Loop

A while loop is similar to the loop loop except that you declare the condition that will break out of the loop upfront.

This is an example of using a while loop with a count. Because you declare the condition that breaks the loop upfront, the intention of your code is much clearer, making this code easier to read than our loop loop above.

You can also use while loops to repeatedly ask a question of the user until they give the desired response.

Until Loop

The until loop is the opposite of the while loop. A while loop continues for as long as the condition is true, whereas an until loop continues for as long as the condition is false. These two loops can therefore be used pretty much interchangeably. Ultimately, what your break condition is will determine which one is more readable.

As much as possible, you should avoid negating your logical expressions using ! (not). First, it can be difficult to actually notice the exclamation point in your code. Second, using negation makes the logic more difficult to reason through and therefore makes your code more difficult to understand. These situations are where until shines.

We can re-write our while loop examples using until.

You can see here that using until means that the loop will continue running until the condition i >= 10 is true.

The next example shows how you can use until to avoid the negation ! that the above while loop had to use.

For Loop

A for loop is used to iterate through a collection of information such as an array or range. These loops are useful if you need to do something a given number of times while also using an iterator.

Times Loop

If you need to run a loop for a specified number of times, then look no further than the trusty times loop. It works by iterating through a loop a specified number of times and even throws in the bonus of accessing the number it’s currently iterating through.

I’m sure you can guess what that code does. Ruby is easily readable that way!

Remember, loops will start counting from a zero index unless specified otherwise, so the first loop iteration will output Alternative fact number 0.

Upto and Downto Loops

The Ruby methods Upto and Downto do exactly what you’d think they do from their names. You can use these methods to iterate from a starting number either up to or down to another number, respectively.

--

--