How does rescue matchers work in Ruby and how can we hack it
1 min read
Ruby uses the === operator to match exceptions
So, when we write rescue FooError, ruby tests the exception object like this:
FooError === exception
How to create custom exception matchers
class FoobarMatcher def self.===(exception) # rescue all exceptions with messages starting with FOOBAR exception.message =~ /^FOOBAR/ endendbegin raise EOFError, "FOOBAR: there was an eof!"rescue FoobarMatcher puts "rescued!"end
Syntactic sugar - Ruby always provides better ways to do things 😉
def exceptions_matching(&block) Class.new do def self.===(other) @block.call(other) end end.tap do |c| c.instance_variable_set(:@block, block) endendbegin raise "FOOBAR: We're all doomed!"rescue exceptions_matching { |e| e.message =~ /^FOOBAR/ } puts "rescued!"end