From: Daniel Berger Date: 2010-02-23T04:39:19+09:00 Subject: Re: Ruby Idiom To Retry open() Upto N Times Before Giving Up On Feb 22, 11:53 am, Dan wrote: > I write a lot of scripts that I run once or infrequently that uses open-rui. Occasionally I run into a web site that times out or is unavailable for a period of time. My normal solution is to just rerun the script manually at a later time and the problem goes away. What I would like to start doing is rescuing the open() and retrying the open() after a delay. What would be the most idiomatic way to try opening a url say n times before giving up? Here is my crude code for trying twice: > > begin  >    open("http://www.example.com/foo.html") >  rescue  >    begin  >       open("http://www.example.com/foo.html") >     rescue  >       #tried twice - giving up  >    end  > end  > > 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. gem install attempt # Try twice, 10 seconds apart attempt(2, 10){ open('http://www.example.com/foo.html') } Regards, Dan