Ruby retry helper with capped jittered backoff

Ruby
def with_retries(max_attempts: 5, base: 0.1, cap: 5.0)
  attempt = 0

  begin
    attempt += 1
    yield attempt
  rescue StandardError
    raise if attempt >= max_attempts
    delay = [cap, base * (2**(attempt - 1))].min
    sleep(delay * (0.5 + rand * 0.5))
    retry
  end
end