Skip to content

Instantly share code, notes, and snippets.

@jtrim
Last active October 21, 2015 21:08
Show Gist options
  • Save jtrim/2f646ee98ca9e67977ef to your computer and use it in GitHub Desktop.
Save jtrim/2f646ee98ca9e67977ef to your computer and use it in GitHub Desktop.
A (mostly) referentially-transparent retry method in Ruby that doesn't mutate local state.
def with_retry(timeout=0.25, tries=3)
exception = \
tries.times.reduce(nil) do
begin
result = yield
rescue StandardError => ex
sleep timeout
ex
else
return result
end
end
raise exception
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment