Skip to content

Instantly share code, notes, and snippets.

@adam12
Created August 26, 2020 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adam12/35071cf695f95a03339e010115516e55 to your computer and use it in GitHub Desktop.
Save adam12/35071cf695f95a03339e010115516e55 to your computer and use it in GitHub Desktop.
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