From: Marcelo Date: 2010-02-23T04:13:28+09:00 Subject: Re: Ruby Idiom To Retry open() Upto N Times Before Giving Up On Mon, Feb 22, 2010 at 12:53, Dan wrote: > I would like a more general approach using ruby idioms where I could specify the maximum number of attempts and delay before giving up. Thanks for your help. Something along the lines of: def open_with_retry(url, n=10, delay=1) 1.upto(n) do begin open(url) do |handle| yield handle end break rescue puts "Sleeping ..." sleep delay end end end open_with_retry('http://does.not.resolve.tld/', 5, 2) do |h| puts "Doing something with #{h.inspect}" end Note that this sleeps even if the last attempt failed. Marcelo