Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Last active December 14, 2015 06:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shinpeim/5044932 to your computer and use it in GitHub Desktop.
Save Shinpeim/5044932 to your computer and use it in GitHub Desktop.
# -*- 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