Skip to content

Instantly share code, notes, and snippets.

@joshnesbitt
Created February 5, 2010 18:12
Show Gist options
  • Select an option

  • Save joshnesbitt/296045 to your computer and use it in GitHub Desktop.

Select an option

Save joshnesbitt/296045 to your computer and use it in GitHub Desktop.
class Integer
def tries(options={}, &block)
attempts = self
exception_classes = options[:on] || Exception
exception_classes = exception_classes.is_a?(Array) ? exception_classes : [exception_classes]
begin
return yield
rescue *exception_classes
retry if (attempts -= 1) > 0
end
yield
end
end
i = 0
3.tries :on => NoMethodError do
puts i += 1
Object.hello
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment