From: "Jesús Gabriel y Galán" Date: 2013-01-25T02:29:04+09:00 Subject: Re: How to DRY this up? On Thu, Jan 24, 2013 at 6:18 PM, tamouse mailing lists wrote: > I have some API calls I'm making, all with the following sort of > begin-rescue-end wrapper: > > (pseudo-code) > > retries = 0 > begin > response = SomeAPI.api_method1(:request => { 'ThisId' => this_id }) > rescue API::Exception => e > retries.succ > sleep( retries * 5 ) > retry unless retries > 2 > end > > where there are multiple api_methods and request forms. Seems like I > should be able to DRY this instead of doing this everywhere I have to > make this sort of API call. You could create a method for it (untested): def call_with_retries obj, method, *args, retries=2 begin response = obj.send(method, *args) rescue API::Exception => e retries -= 1 sleep( retries * 5 ) retry unless retries < 0 end end and call it like call_with_retries SomeApi, :api_method1, :request => { 'ThisId' => this_id } You could also pass the exception to rescue, or just rescue StandardError, and also pass the time to sleep. Hope this gives you some ideas. Jesus.