Created
January 16, 2012 23:11
-
-
Save eegrok/1623520 to your computer and use it in GitHub Desktop.
interesting error catching method / singleton_class (class << self) stuff
This file contains hidden or 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
| 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