lib/rext/object/helpers.rb in visionmedia-rext-0.2.2 vs lib/rext/object/helpers.rb in visionmedia-rext-0.3.0

- old
+ new

@@ -36,6 +36,27 @@ def returning value, &block yield value value end + ## + # Retry a _block_ of statements upto a number of _times_. + # When no error is raised the value returned by _block_ is + # simply returned. + # + # === Examples + # + # try 3 do + # open 'http://vision-media.ca' + # end + # # => allows 3 tries to fetch the response + # + + def try times = 1, options = {}, &block + val = yield + rescue options[:on] || Exception + retry if (times -= 1) > 0 + else + val + end + end \ No newline at end of file