Last active
December 14, 2015 06:48
-
-
Save Shinpeim/5044932 to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
class Retry | |
def initialize(times, begin_block) | |
@times = times | |
@begin_block = begin_block | |
end | |
def catch(error_type, &rescue_block) | |
tries = 0 | |
begin | |
@begin_block.call(tries) | |
rescue error_type => e | |
rescue_block.call(e, tries) | |
tries += 1 | |
retry if tries < @times | |
end | |
end | |
end | |
class Fixnum | |
def times_try(&block) | |
return Retry.new(self, block) | |
end | |
end | |
class CustomError < StandardError;end | |
10.times_try {|i| | |
p "#{i}: せーのっ" | |
raise CustomError | |
p "#{i}: でもそんなーんじゃっだーめっ" | |
} .catch(CustomError) {|e, i| | |
p "#{i}: ...?" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment