From: Rick DeNatale Date: 2010-02-23T04:24:15+09:00 Subject: Re: Ruby Idiom To Retry open() Upto N Times Before Giving Up On Mon, Feb 22, 2010 at 1:53 PM, 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. > begin raise "oops" rescue retry_count = (retry_count || 0) + 1 puts "retry #{retry_count}" retry unless retry_count >= 5 end -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale