Last active
          November 1, 2019 22:39 
        
      - 
      
- 
        Save movstox/46b7f5011b61a9d068d23114865af946 to your computer and use it in GitHub Desktop. 
    catch Exception and retry x times
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def with_catch_and_retry(*exceptions_to_catch, times, &block) | |
| block.call | |
| rescue *exceptions_to_catch => e | |
| (times -= 1).zero? ? raise(e) : retry | |
| end | |
| context = 'nice' | |
| with_catch_and_retry(Exception, RuntimeError, 3) { | |
| p "123: #{context}" | |
| raise 'something' | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment