-
-
Save adam12/35071cf695f95a03339e010115516e55 to your computer and use it in GitHub Desktop.
This file contains 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
require "delegate" | |
class ClassMap < DelegateClass(Hash) | |
def [](klass) | |
__getobj__.fetch(klass) { | |
__getobj__.each { |key, value| | |
return value if klass == key.to_s | |
} | |
nil | |
} | |
end | |
end | |
map = ClassMap.new({ | |
Integer => "1", | |
String => "2" | |
}) | |
pp map[String] | |
pp map[Integer] | |
pp map["String"] | |
pp map["Integer"] | |
pp map["foo"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment