Module: Retrying

Defined in:
lib/retrying.rb,
lib/retrying/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#retrying(options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/retrying.rb', line 4

def retrying(options = {}, &block)
  options = { :on => StandardError, :tries => 2, :sleep => 0 }.merge(options)
  exceptions = Array(options[:on])

  attempts = 0
  begin
    yield
  rescue *exceptions => ex
    attempts += 1
    raise if attempts >= options[:tries]
    sleep(options[:sleep]) if options[:sleep] > 0
    retry
  end
end