From: Gennady Date: 2003-11-24T16:25:26+09:00 Subject: Re: A code snippet: Controlled retry On Nov 23, 2003, at 23:02, Gennady wrote: > > On Nov 23, 2003, at 22:11, Hal Fulton wrote: > >> def try(quiet=true,times=5,secs=1,&block) >> count = 0 >> catch :finished do >> begin >> block.call >> rescue => err >> puts "Error was: #{err}" if not quiet >> count += 1 >> throw :finished if count > times >> sleep secs >> retry >> end >> end >> end > > > def try(quiet=true,times=5,secs=1,&block) > return unless block > times.times do > begin > return block.call > rescue => err > puts "Error was: #{err}" unless quiet > sleep secs > end > end > end > Oops, times.times should become times.next.times to function like the original. Also, when all attempts are exhausted, the last exception must be re-raised or another more appropriate one raised (boolean return would also do the trick).