[ACCEPTED]-sleep until condition is true in ruby-ruby
Accepted answer
until
can be a statement modifier, leading to:
sleep(1) until ready_to_go
You'll 7 have to use that in a thread with another 6 thread changing ready_to_go
otherwise you'll hang.
while (!ready_to_go)
sleep(1)
end
is 5 similar to that but, again, you'd need something 4 to toggle ready_to_go
or you'd hang.
You could use:
until (ready_to_go)
sleep(1)
end
but 3 I've never been comfortable using until
like 2 that. Actually I almost never use it, preferring 1 the equivalent (!ready_to_go)
.
You can use the waitutil
gem as described at http://rubytools.github.io/waitutil/, e.g.
require 'waitutil'
WaitUtil.wait_for_condition("my_event to happen",
:timeout_sec => 30,
:delay_sec => 0.5) do
check_if_my_event_happened
end
0
def sleep_until(time)
time.times do
break if block_given? && yield
sleep(1)
end
end
Usage:
sleep_until(18){till_i_die}
0
I like this form since it is simple and 2 only uses sleep if needed after it tests 1 for the done condition:
begin
ready_to_go = do_some_action
end until ready_to_go or not sleep 1
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.