HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyCritical

Tell Ruby Program to Wait some amount of time

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
programsometimewaitrubyamounttell

Problem

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

Solution

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

Code Snippets

sleep(num_secs)
sleep(4.minutes)
# or, even longer...
sleep(2.hours); sleep(3.days) # etc., etc.
# or shorter
sleep(0.5) # half a second

Context

Stack Overflow Q#1329967, score: 808

Revisions (0)

No revisions yet.