Here is another interesting alternative. Can be adapted to what you want.
Pasting most interesting part:
def match_message(regexp) lambda{ |error| regexp === error.message } end begin raise StandardError, "Error message about a socket." rescue match_message(/socket/) => error puts "Error #{error} matches /socket/; ignored." end
See the original site for ruby 1.8.7 solution.
It turns out lambda not accepted my more recent ruby versions. It seems the option is to use what worked in 1.8.7 but that's IM slower (to create a new class in all comparisons. So I don't recommend using it and have not even tried it:
def exceptions_matching(&block) Class.new do def self.===(other) @block.call(other) end end.tap do |c| c.instance_variable_set(:@block, block) end end begin raise "FOOBAR: We're all doomed!" rescue exceptions_matching { |e| e.message =~ /^FOOBAR/ } puts "rescued!" end
If somebody knows when ruby removed lambda support in rescue please comment.
Errnonamespace specifically, or 2) unaccept the answer.