Last active
June 19, 2018 14:33
-
-
Save smaximov/50ed7dbc35adb3b2461b8df7538239bc 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
class MessageMatcher < Module | |
def initialize(pattern) | |
@pattern = pattern | |
end | |
def ===(exception) | |
exception.message.match?(@pattern) | |
end | |
end | |
def message_matcher(msg) | |
Module.new.tap do |mod| | |
mod.define_singleton_method(:===) { |exc| exc.message.match?(msg) } | |
end | |
end | |
begin | |
raise 'Foo' | |
rescue MessageMatcher.new(/foo/i) => e # rescue message_matcher(/foo/) | |
puts "rescued #{e}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment