Creating an infinite loop

Andrew Walz picture Andrew Walz · Nov 26, 2014 · Viewed 19.5k times · Source

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?

Answer

Todd A. Jacobs picture Todd A. Jacobs · Nov 26, 2014
loop do
  puts 'foo'  
  puts 'bar'  
  sleep 300
end