Skip to content

Instantly share code, notes, and snippets.

@oki
Created November 25, 2009 14:34
Show Gist options
  • Select an option

  • Save oki/242748 to your computer and use it in GitHub Desktop.

Select an option

Save oki/242748 to your computer and use it in GitHub Desktop.
# Based on from: http://blog.codefront.net/2008/01/14/retrying-code-blocks-in-ruby-on-exceptions-whatever/
def retryable_deluxe(options = {}, &block)
opts = { :tries => 1, :on => { :exception => Exception, :return => false } }.merge(options)
retry_opt, retries = opts[:on], opts[:tries]
retry_exception = retry_opt[:exception]
retry_return = retry_opt[:return]
retries_max = retries + 1
begin
puts "Attempt: #{retries_max-retries}"
return yield == retry_return && raise(retry_exception,"#{retry_return}")
rescue retry_exception => e
retry if (retries -= 1) > 0
raise "Zonk: #{e}"
end
end
retryable_deluxe(:tries => 3, :on => { :exception => StandardError, :return => false }) do
# puts "okej"
# raise "ble"
false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment