Created
January 17, 2016 15:44
-
-
Save tatat/4c4d00c14f2ee4e56015 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
module Rescuer | |
class << self | |
attr_accessor :error_class | |
def error_matcher | |
error_class = self.error_class | |
Module.new do | |
define_singleton_method :=== do |e| | |
error_class === e | |
end | |
end | |
end | |
end | |
end | |
class ErrorSetBeforeDefinition < StandardError; end | |
class ErrorSetAfterDefinition < StandardError; end | |
Rescuer.error_class = ErrorSetBeforeDefinition | |
def try_rescue(error_class) | |
raise error_class | |
rescue Rescuer.error_matcher => e | |
end | |
try_rescue ErrorSetBeforeDefinition #=> Nothing was raised | |
Rescuer.error_class = ErrorSetAfterDefinition | |
try_rescue ErrorSetAfterDefinition #=> Nothing was raised |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment