23

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?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Andrew Walz
  • 890
  • 2
  • 8
  • 27
  • 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 Nov 26 '14 at 01:49
  • 1
    Thank you for clarifying. This loop is not being used for rails. I have removed the tag. Thanks for the explanation! – Andrew Walz Nov 26 '14 at 02:18

4 Answers4

45
loop do
  puts 'foo'  
  puts 'bar'  
  sleep 300
end
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • Upvoted for a correct answer, but the best answer is probably still @meagar's in question comments. :) – Amadan Nov 26 '14 at 02:10
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
vgoff
  • 10,980
  • 3
  • 38
  • 56
1

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
Rahul Dess
  • 2,397
  • 2
  • 21
  • 42
  • 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 Nov 26 '14 at 02:08
  • 4
    The recursion alternative will not work as with each iteration you're adding a level to the stack and eventually a [`SystemStackError`](http://www.ruby-doc.org/core-2.1.5/SystemStackError.html) will be raised. – Gabriel de Oliveira Nov 26 '14 at 02:25