Sha256: 16eecb8e4d64177c7cce35c4cac14c067468e34909c7704d72424cf4f0ff0a11

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

# We're monkey patching Enumerable Directly

# See Enumerable
module Rrrretry
end

module Enumerable
  # Attempts to run code for every element, returns the first valid run.
  # If code does not return, the original error will be raised.
  #
  # First run returns a `divided by 0` error, so the next result is returned.
  #
  #  [0, 1, 2].each.retry { |i| 1/i }
  #  # => 1
  #
  # If there are no valid returns, the last error will be raised.
  #
  #  [0, 1, 2].each.retry { raise "bar" }
  #  # => RuntimeError: bar
  #
  # By default only `StandardError` is caught, multiple error types can be
  # specified.
  #
  #  array = [->{ raise Exception }, ->{ raise SignalException }, ->{ 1 }]
  #  array.each.retry(Exception, SignalException) { |i| i.call }
  #  # => 1
  def retry(*exceptions, &block)
    exceptions << StandardError if exceptions.empty?
    enum       ||= self.to_enum
    yield enum.next
  rescue *exceptions => e
    last_exception = e and retry unless e.is_a? StopIteration
    raise last_exception unless last_exception.nil?
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rrrretry-0.0.1 lib/rrrretry.rb