告诉 Ruby 程序等待一段时间

发布于 2024-08-02 09:45:50 字数 38 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

挽袖吟 2024-08-09 09:45:50

像这样:

sleep(num_secs)

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

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

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

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
才能让你更想念 2024-08-09 09:45:50

像这样使用 sleep

sleep 2

这将休眠 2 秒。

小心地提出论点。如果您只是运行 sleep,该进程将永远休眠。 (当您希望线程休眠直到被唤醒时,这非常有用。)

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

∞梦里开花 2024-08-09 09:45:50

我发现 until 对于睡眠非常有用。例子:

> 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

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
别念他 2024-08-09 09:45:50

像这样

sleep(no_of_seconds)

或者您可以传递其他可能的参数,例如:

sleep(5.seconds)

sleep(5.minutes)

睡眠(5.小时)

睡眠(5.天)

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)

握住你手 2024-08-09 09:45:50

秒/分钟/小时的实现,这是rails方法。请注意,隐式返回不是必需的,但它们看起来更干净,所以我更喜欢它们。我不确定 Rails 是否有 .days 或者是否更进一步,但这些是我需要的。

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

之后,您可以执行以下操作:
sleep 5.seconds 睡眠 5 秒。您可以sleep 5.mines来睡5分钟。您可以sleep 5.hours来睡5个小时。最后,您可以执行 sleep 5.days 来睡眠 5 天...您可以添加任何返回 self * 值(该时间范围内的秒数)的方法。
作为练习,尝试实施几个月!

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!

暗恋未遂 2024-08-09 09:45:50

sleep 6 将休眠 6 秒。如果需要更长的持续时间,您还可以使用 sleep(6.minutes)sleep(6.hours)

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

韶华倾负 2024-08-09 09:45:50

的示例。

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

这是使用 sleepsidekiq sleep 10 秒并打印出 "Working super hard"

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文