Skip to content

Instantly share code, notes, and snippets.

@tatat
Created January 17, 2016 15:44
Show Gist options
  • Save tatat/4c4d00c14f2ee4e56015 to your computer and use it in GitHub Desktop.
Save tatat/4c4d00c14f2ee4e56015 to your computer and use it in GitHub Desktop.
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