告诉 Ruby 程序等待一段时间 [英] Tell Ruby Program to Wait some amount of time

查看:56
本文介绍了告诉 Ruby 程序等待一段时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何告诉 Ruby 程序在进入下一行代码之前等待任意时间?

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

推荐答案

像这样:

sleep(num_secs)

num_secs 值可以是整数或浮点数.

The num_secs value can be an integer or float.

此外,如果您在 Rails 应用中编写此代码,或者在项目中包含 ActiveSupport 库,则可以使用以下便捷语法构建更长的间隔:

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

这篇关于告诉 Ruby 程序等待一段时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆