Skip to content
Browse files

added retry_with: parameter to Integer#times_try:retry_with:

  • Loading branch information...
1 parent 6e06162 commit 3f4248cee8fb507beb6efb4f001e476b88aa3344 @bakkdoor committed
Showing with 7 additions and 2 deletions.
  1. +4 −2 lib/integer.fy
  2. +3 −0 tests/fixnum.fy
View
6 lib/integer.fy
@@ -17,9 +17,10 @@ class Integer {
}
}
- def times_try: block {
+ def times_try: block retry_with: retry_block ({}) {
"""
@block @Block@ to be called and retries @self amount of times if exceptions get thrown.
+ @retry_block @Block@ to be called before retrying execution of @block. Defaults to an empty @Block@.
@return Return value of calling @block or raises an exception after @self tries.
Returns @nil if @self <= 0.
@@ -32,7 +33,7 @@ class Integer {
# if it succeeds the first time, simply return its value, otherwise try once more.
# if it still fails after 2 attempts, raises the exception thrown (in this case probably an IOError).
@connection readln
- }
+ } retry_with: { @connection reconnect }
"""
max_retries = self
@@ -43,6 +44,7 @@ class Integer {
} catch Exception => e {
max_retries = max_retries - 1
{ e raise! } unless: $ max_retries > 0
+ retry_block call
retry
} finally {
return value
View
3 tests/fixnum.fy
@@ -135,5 +135,8 @@ FancySpec describe: Fixnum with: {
tries is: 2
2 times_try: { 2 } . is: 2
+
+ i = 0
+ 2 times_try: { 2 / i } retry_with: { i = 1 } . is: 2
}
}

0 comments on commit 3f4248c

Please sign in to comment.
Something went wrong with that request. Please try again.