rails/rails — pull request #8561 by schneems (2012-12-19 18:06:55+00:00)
Title: Ability to Retry code a given number of times

Ruby gives us the ability to retry a piece of code, but the mechanisms to control or limit the number of times it is retried must be manually coded each time by a developer. This PR gives the ability to retry arbitrary code a maximum number of times based on an enumerator. If an exception is raised on every round of the enumerator, the last exception before StopIteration was be raised.

This functionality is particularly useful for executing non-deterministic actions, such as networking code where one successful call is required, but an infinite number of retries is not desired since the endpoint may be permanently down.

```
3.times.retry do |i|
puts "foo #{i}"
raise "bar"
end
# foo 0
# foo 1
# foo 2
# RuntimeError: bar

3.times.retry do |i|
puts "foo #{i}"
end
# foo 0
# => nil
```

WDYT?