24

I'm trying to create an infinite loop, where a block of code will be executed forever.

All loop documentation I have found warns against creating an infinite loop, but no examples of a working one.

If I have a block of code:

{ puts "foo"  
  puts "bar"  
  sleep 300 }

How would I go about running this block forever?

2
  • 7
    You've tagged this ruby-on-rails. If you're trying to create an infinite loop in Rails, you're probably doing something horribly wrong. You can't do that in the same process that is serving your site, or the single thread of execution cannot actually respond to incoming requests. If you want to do something every 300 seconds, you need an asynchronous background job. You should describe your actual problem so we can provide you with real advice - using an infinite loop is a solution, not a problem.
    – user229044
    Commented Nov 26, 2014 at 1:49
  • 1
    Thank you for clarifying. This loop is not being used for rails. I have removed the tag. Thanks for the explanation! Commented Nov 26, 2014 at 2:18

4 Answers 4

46
loop do
  puts 'foo'  
  puts 'bar'  
  sleep 300
end
0
24

Here are some examples of infinite loops using blocks.

Loop

loop do
  puts "foo"  
  puts "bar"  
  sleep 300
end

While

while true
  puts "foo"  
  puts "bar"  
  sleep 300
end

Until

until false
  puts "foo"  
  puts "bar"  
  sleep 300
end

Lambda

-> { puts "foo" ; puts "bar" ; sleep 300}.call until false

There are a few variations of the lambda as well, using the non-stabby lambda syntax. Also we could use a Proc.

Begin..End

begin
  puts "foo"  
  puts "bar"
  sleep 300
end while true
2

I have tried all but with inputs loop only worked as infinty loop till I get a valid input:

loop do
    a = gets.to_i
    if (a >= 2)
        break
    else 
        puts "Invalid Input, Please enter a correct Value >=2: "
    end
end
-6

1) While loop:

While 1==1 # As the condition 1 is equal to 1 is true, it always runs.
    puts "foo"  
    puts "bar"  
    sleep 300
   end

2) Recursion :

    def infiniteLoop # Using recursion concept
      puts "foo"
      puts "bar"
      sleep 300
      infiniteLoop #Calling this method again    
    end

EDIT : I thought this would work, but as Gabriel mentioned, we would get SystemStackError.

3) Loop

   loop do
    puts "foo"
    ....
   end

4) Using unless

unless 1 == 2  # Unless 1 is equal to 2 , it keeps running
 puts "foo"
 ...
end
2
  • 6
    unless is not a loop construct, it is a synonym for if not. You might have been thinking of until, a synonym for while not. And while we're on while, Ruby is case-sensitive: While is an undefined constant, and not a keyword like while is; and as a keyword, it does not accept a block, so while ... do is a syntax error as well.
    – Amadan
    Commented Nov 26, 2014 at 2:08
  • 4
    The recursion alternative will not work as with each iteration you're adding a level to the stack and eventually a SystemStackError will be raised. Commented Nov 26, 2014 at 2:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.