• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Ruby Infinite Loop

Ruby Infinite Loop

October 18, 2022 Leave a Comment

A Ruby infinite loop is one of the most dreaded errors when coding in Ruby because the code inside the loop will just run forever. This can be dangerous if there is a lot of code inside the loop, as it will use a ton of your computer’s resources and usually cause a crash.

Here are two examples of an infinite loop in Ruby. One in a do loop and one in a while loop.

i = 1
loop do
  puts i
  break if i < 1
  i += 1
  #This code will never stop
end
i = 1
while  i > 0 
  #The code inside this while loop will never stop
  puts i
  i += 1
end

In the do loop above, the code will generate an infinite loop because the variable i is set to the number 1 to start. You then have the natural progression of increasing the value of the number until it reaches a condition that will not be true, our break condition that will stop the loop. However, in the code above, that condition is that i < 1. Since i (which is 1) is equal to 1, we add 1 to it and try again. We get 2 < 1, which is false. We then repeat the step until, well forever. Because we are adding 1 each time, the condition i < 1 will never be true, and so the loop will never break and run forever.

The second example is basically the same as the do loop. The while condition of i > 0 will always be true and so the code inside the while loop will run forever.

Let’s take a look at one of these in action.

Ruby Infinite Loop in Action, Kind Of

In this example, we will run an infinite loop, well kind of. Because we don’t want to crash your computer, we will show the start of an infinite loop, and add some safety code to make sure you always avoid infinite loops.

The Ruby code for this example will be pretty simple. We will run the while infinite loop example from above with some code in it. We will print some code so you can see how many times the code will run.

Like we said at the start, since we don’t want to do any damage, we will put a safety check in place. We will not let this code run more than 1000 times. We can set this number to some number we know should not be reached if the code runs correctly. So we will add some code into the while loop and add a conditional check to make sure we avoid the infinite loop.

Here is the Ruby code:

i = 1
infinite_loop_safety_check = 0
  while( i > 0 && infiniteLoopSafetyCheck < 1000 ){
    puts i
    i+= 1
    infiniteLoopSafetyCheck += 1
  end

#Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
1000

Hopefully this article has been useful for you to understand the Ruby infinite loop.

Other Articles You'll Also Like:

  • 1.  Ruby Floor – Find Floor of Number with floor Method
  • 2.  Ruby Ceiling – Find the Ceiling of a Number with the ceil Method
  • 3.  Using Ruby to Create List of Prime Numbers
  • 4.  Ruby unshift Method – Add Items to Start of Array
  • 5.  How to Square a Number in Ruby
  • 6.  Ruby has_key Method – Check if Hash Has Key
  • 7.  Ruby Round – Round Number to Nearest Integer with the round Method
  • 8.  How to Print With Newline in Ruby
  • 9.  How to Print Without Newline in Ruby
  • 10.  Get the Sum of an Array in Ruby

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy