sleep until condition is true in ruby

后端 未结 4 1662
盖世英雄少女心
盖世英雄少女心 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:43

    def sleep_until(time)
      time.times do
        break if block_given? && yield
        sleep(1)
      end
    end
    

    Usage:

    sleep_until(18){till_i_die}
    

提交回复
热议问题