Tell Ruby Program to Wait some amount of time

RubySleep

Ruby Problem Overview


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

Ruby Solutions


Solution 1 - Ruby

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

Solution 2 - Ruby

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.)

Solution 3 - Ruby

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

Solution 4 - Ruby

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)

Solution 5 - Ruby

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!

Solution 6 - Ruby

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

Solution 7 - Ruby

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" .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser94154View Question on Stackoverflow
Solution 1 - RubyrcoderView Answer on Stackoverflow
Solution 2 - RubyClinton DreisbachView Answer on Stackoverflow
Solution 3 - RubyVarun GargView Answer on Stackoverflow
Solution 4 - RubywajeehView Answer on Stackoverflow
Solution 5 - RubygsbView Answer on Stackoverflow
Solution 6 - Rubyvijaya chowdaryView Answer on Stackoverflow
Solution 7 - RubyAsyrafView Answer on Stackoverflow