Close

Ruby – Creating an infinite loop

You can also create an infinite loop using the “loop” key word and the “do…end” block:

Announcement

I have released my new course on Udemy, Kubernetes By Example. Sign up now to get free lifetime access!
loop do
  puts Time.now
  sleep(5)        # 5 second sleep 
end

This will output something like:

irb(main):001:0> exit
PS C:\Temp\irb> ruby .\loop.rb
2014-11-26 10:14:08 +0000
2014-11-26 10:14:13 +0000
2014-11-26 10:14:18 +0000
2014-11-26 10:14:23 +0000
.
.
.
....etc

You can also achieve the same outcome by setting up a while-true loop.