Thursday, February 25, 2016

retry

attempts = 0
begin
  raise
rescue
  attempts += 1
  puts 'retrying...'
  retry if attempts < 3
end


def work
  with_retry do
    puts 'working...'
    raise
  end
end

def with_retry
  attempts = 0
  begin
    yield
  rescue
    attempts += 1
    puts 'retrying...'
    if attempts < 3
      retry
    else
      puts 'Giving up!'
    end
  end
end

work