479

How do you tell a Ruby program to wait an arbitrary amount of time before moving on to the next line of code?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user94154
  • 16,176
  • 20
  • 77
  • 116

7 Answers7

776

Like this:

sleep(num_secs)

The num_secs value can be an integer or float.

Also, if you're writing this within a Rails app, or have included the ActiveSupport library in your project, you can construct longer intervals using the following convenience syntax:

sleep(4.minutes)
# or, even longer...
sleep(2.hours); sleep(3.days) # etc., etc.
# or shorter
sleep(0.5) # half a second
user664833
  • 18,397
  • 19
  • 91
  • 140
rcoder
  • 12,229
  • 2
  • 23
  • 19
140

Use sleep like so:

sleep 2

That'll sleep for 2 seconds.

Be careful to give an argument. If you just run sleep, the process will sleep forever. (This is useful when you want a thread to sleep until it's woken.)

Clinton Dreisbach
  • 7,732
  • 6
  • 29
  • 27
11

I find until very useful with sleep. example:

> time = Time.now
> sleep 2.seconds until Time.now > time + 10.seconds # breaks when true
> # or
> sleep 2 and puts 'still sleeping' until Time.now > time + 10
> # or
> sleep 1.seconds until !req.loading # suggested by ohsully
Varun Garg
  • 2,464
  • 23
  • 37
  • 4
    This is the same thing as `sleep(10.seconds)` – danielricecodes May 31 '20 at 14:36
  • Yeah, here it is the same because it uses the same input (time) as offered by sleep. I wanted to highlight how `until` can be used to sleep until any condition is achieved (also love the how natural the syntax feels) – Varun Garg May 31 '20 at 16:07
  • This makes my head hurt. Can you provide a different example than this to showcase the value of using `until` here instead of just `sleep(10.seconds)`? – Joshua Pinter Jan 03 '21 at 16:54
  • 1
    Suppose you wanted to block until a request returns: `sleep 1.seconds until !req.loading` – ohsully Jan 04 '21 at 10:11
  • This is not exactly the same thing as sleep(10). The kernel may wake the thread early. So if you *must* sleep for at least 10 seconds, you need a construct like this. See https://apidock.com/ruby/Kernel/sleep – MZB Jan 20 '21 at 22:43
  • @MZB you are right. But in case you want to call `Thread#run` from another thread, you might just place that condition in until which will give more ownership to the sleeping thread. Might be totally wrong here – Varun Garg Jan 21 '21 at 10:15
7

Like this

sleep(no_of_seconds)

Or you may pass other possible arguments like:

sleep(5.seconds)

sleep(5.minutes)

sleep(5.hours)

sleep(5.days)

wajeeh
  • 87
  • 1
  • 3
2

Implementation of seconds/minutes/hours, which are rails methods. Note that implicit returns aren't needed, but they look cleaner, so I prefer them. I'm not sure Rails even has .days or if it goes further, but these are the ones I need.

class Integer
   def seconds
      return self
   end
   def minutes
      return self * 60
   end
   def hours
      return self * 3600
   end
   def days
      return self * 86400
   end
end

After this, you can do: sleep 5.seconds to sleep for 5 seconds. You can do sleep 5.minutes to sleep for 5 min. You can do sleep 5.hours to sleep for 5 hours. And finally, you can do sleep 5.days to sleep for 5 days... You can add any method that return the value of self * (amount of seconds in that timeframe). As an exercise, try implementing it for months!

gsb
  • 67
  • 2
  • 9
0

sleep 6 will sleep for 6 seconds. For a longer duration, you can also use sleep(6.minutes) or sleep(6.hours).

Jon
  • 2,932
  • 2
  • 23
  • 30
  • 4
    Not sure about the downvotes on this, but it could be that `minutes()` and `hours()` are methods on numerics added by Ruby on Rails - so not standard available in Ruby - in the ActiveSupport::Duration class. They're quite convenient though. – Jochem Schulenklopper Sep 18 '17 at 08:00
  • 13
    I think the primary reason for downvotes is that @vijaya_chowdary basically reposted the voted correct answer 8 years later. I would consider it is strange at the least... – StahlRat May 18 '18 at 17:37
  • 1
    I downvoted for exactly that. It's not standard ruby. – hayesk Sep 05 '18 at 17:37
-1

This is an example of using sleep with sidekiq

require 'sidekiq'

class PlainOldRuby
  include Sidekiq::Worker

  def perform(how_hard="super hard", how_long=10)
    sleep how_long
    puts "Workin' #{how_hard}"
  end
end

sleep for 10 seconds and print out "Working super hard" .

Asyraf
  • 609
  • 6
  • 14