Created
April 15, 2018 00:20
-
-
Save kxtxr/ec22f98522a9edd60157017325f4c11e to your computer and use it in GitHub Desktop.
Ruby repeat a block every n seconds for timeout_in seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def repeat_every(n = 10.seconds, timeout_in = 60.seconds) | |
begin | |
Timeout.timeout(timeout_in_seconds) do | |
loop do | |
yield false if block_given? | |
sleep n | |
end | |
end | |
rescue Timeout::Error | |
yield true if block_given? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment