Last Updated: February 25, 2016
·
1.833K
· zhekanax

Ruby retry with limit

lib/core_ext/object.rb:

class Object
  def with_retry(limit)
    limit.times do |i|
      begin
        return yield i
      rescue => e
        raise e if i + 1 == limit
      end
    end
  end
end

Use case:

with_retry(5) do
  make_remote_call
  or_smth_other
end