Hi all,

I'm tired of this idiom:

max = 3
begin
    Timeout.timeout(val){
       # some op that could fail or timeout on occasion
    }
rescue Exception
    max -= 1
    if max > 0
       sleep interval
       retry
    end
    raise
end

Mark Fowler wrote a Perl module called "attempt" 
(http://search.cpan.org/~markf/Attempt-1.01/lib/Attempt.pm) that I think 
is pretty handy, and I would like this for myself.  I figure the API 
should look like this:

# 1st arg is retries, 2nd arg is interval
attempt(3, 300){
    FTP.open(host, user, passwd){ ... }
}

Here's my possibly naive implementation:

require 'timeout'

module Kernel
    def attempt(tries = 3, interval = 60, timeout = nil)
       begin
          if timeout
             Timeout.timeout(timeout){ yield }
          else
             yield
          end
       rescue
          tries -= 1
          if tries > 0
             sleep interval
             retry
          end
          raise
       end
    end
end

What do you think?  Useful?  Are there any gotchas I need to consider, 
such as nested begin/end blocks, try/catch?  Anything else?  Should I 
provide some way to provide debug info?  Finer grained error handling?

Ideas welcome.

Thanks,

Dan