Skip to content

Instantly share code, notes, and snippets.

@smaximov
Last active June 19, 2018 14:33
Show Gist options
  • Save smaximov/50ed7dbc35adb3b2461b8df7538239bc to your computer and use it in GitHub Desktop.
Save smaximov/50ed7dbc35adb3b2461b8df7538239bc to your computer and use it in GitHub Desktop.
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