Skip to content

Instantly share code, notes, and snippets.

@etozzato
Created November 9, 2009 12:45
Show Gist options
  • Save etozzato/229914 to your computer and use it in GitHub Desktop.
Save etozzato/229914 to your computer and use it in GitHub Desktop.
# Author: Emanuele Tozzato (mailto:etozzato@gmail.com)
#
# = Description
# Execute given block until true for given attempts.
# = Usage
# exec! { gateway.perform_handshake('2897-XSANI-19827') }
module Exec
CONFIG = {
:default_attempts => 3,
:max_attempts => 10,
:err => "attempts number is too high",
:sleep => 0.5
}
def exec!(attempts=CONFIG[:default_attempts])
attempts = attempts.to_i
raise StandardError.new(CONFIG[:err]) if attempts > CONFIG[:max_attempts]
begin
1.upto(attempts) do |i|
result = yield
return true if result
sleep(CONFIG[:sleep])
end
false
rescue
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment