Created
March 27, 2017 02:34
-
-
Save fujimura/1787902f8ba908a466740326fb84b495 to your computer and use it in GitHub Desktop.
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
module Match | |
def self.define_matchers!(m) | |
m.constants.each {|c| | |
eval "class #{c} < StandardError; end" | |
} | |
end | |
def self.match! obj | |
raise self.const_get(obj.class.to_s).new(obj) | |
end | |
end | |
Match.define_matchers! Object | |
def foo(arg) | |
Match.match! arg | |
rescue Match::String => s | |
p "this is a string" | |
p s | |
rescue Match::Fixnum => i | |
p "this is a Fixnum" | |
p i | |
end | |
foo 1 | |
foo 'bar' |
Author
fujimura
commented
Mar 27, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment