sleep until condition is true in ruby

后端 未结 4 1657
盖世英雄少女心
盖世英雄少女心 2021-02-05 11:12

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
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 11:33

    I like this form since it is simple and only uses sleep if needed after it tests for the done condition:

    begin
      ready_to_go = do_some_action
    end until ready_to_go or not sleep 1
    

提交回复
热议问题