Skip to content

Instantly share code, notes, and snippets.

@kxtxr
Created April 15, 2018 00:20
Show Gist options
  • Save kxtxr/ec22f98522a9edd60157017325f4c11e to your computer and use it in GitHub Desktop.
Save kxtxr/ec22f98522a9edd60157017325f4c11e to your computer and use it in GitHub Desktop.
Ruby repeat a block every n seconds for timeout_in seconds
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