本文介绍了睡眠直到条件在 ruby 中为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Ruby 是否有更好的睡眠方式,直到某些条件成立?
Is there any better way in Ruby to sleep until some condition is true ?
loop do
sleep(1)
if ready_to_go
break
end
end
推荐答案
until
可以是语句修饰符,导致:
until
can be a statement modifier, leading to:
sleep(1) until ready_to_go
您必须在另一个线程更改ready_to_go
的线程中使用它,否则您将挂起.
You'll have to use that in a thread with another thread changing ready_to_go
otherwise you'll hang.
while (!ready_to_go)
sleep(1)
end
与此类似,但同样,您需要切换ready_to_go
或挂起.
is similar to that but, again, you'd need something to toggle ready_to_go
or you'd hang.
您可以使用:
until (ready_to_go)
sleep(1)
end
但我从不习惯像这样使用 until
.实际上我几乎从不使用它,更喜欢等效的 (!ready_to_go)
.
but I've never been comfortable using until
like that. Actually I almost never use it, preferring the equivalent (!ready_to_go)
.
这篇关于睡眠直到条件在 ruby 中为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!