Tell Ruby Program to Wait some amount of time

2019-01-16 00:39发布

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

标签: ruby sleep
3条回答
甜甜的少女心
2楼-- · 2019-01-16 01:22

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

查看更多
smile是对你的礼貌
3楼-- · 2019-01-16 01:30

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
查看更多
神经病院院长
4楼-- · 2019-01-16 01:32

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

查看更多
登录 后发表回答