Skip to content

Instantly share code, notes, and snippets.

@eegrok
Created January 16, 2012 23:11
Show Gist options
  • Select an option

  • Save eegrok/1623520 to your computer and use it in GitHub Desktop.

Select an option

Save eegrok/1623520 to your computer and use it in GitHub Desktop.
interesting error catching method / singleton_class (class << self) stuff
def errors_with_message(pattern)
# Generate an anonymous "matcher module" with a custom threequals m = Module.new
m = Module.new
# next 3 lines are equivalent
# (m.singleton_class.class_eval { self }).instance_eval do
m.singleton_class.instance_eval do
# (class << m; self; end).instance_eval do
define_method(:===) do |e|
pattern === e.message
end
end
m
end
begin
raise 'blaherror'
rescue errors_with_message(/blah/)
puts "caught blah error"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment