Module: Beaker::Shared::Repetition
- Included in:
- Beaker::Shared
- Defined in:
- lib/beaker/shared/repetition.rb
Instance Method Summary collapse
- #repeat_fibonacci_style_for(attempts) ⇒ Object
- #repeat_for(seconds, &block) ⇒ Object
- #repeat_for_and_wait(seconds, wait) ⇒ Object
Instance Method Details
#repeat_fibonacci_style_for(attempts) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/beaker/shared/repetition.rb', line 20 def repeat_fibonacci_style_for attempts done = false attempt = 1 last_wait, wait = 0, 1 while not done and attempt <= attempts do done = yield attempt += 1 sleep wait unless done last_wait, wait = wait, last_wait + wait end return done end |
#repeat_for(seconds, &block) ⇒ Object
5 6 7 8 |
# File 'lib/beaker/shared/repetition.rb', line 5 def repeat_for seconds, &block # do not peg CPU if &block takes less than 1 second repeat_for_and_wait seconds, 1, &block end |
#repeat_for_and_wait(seconds, wait) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/beaker/shared/repetition.rb', line 10 def repeat_for_and_wait seconds, wait timeout = Time.now + seconds done = false until done or timeout < Time.now do done = yield sleep wait unless done end return done end |