Skip to content

Instantly share code, notes, and snippets.

@roryokane
Created July 31, 2012 19:58
Show Gist options
  • Save roryokane/3219986 to your computer and use it in GitHub Desktop.
Save roryokane/3219986 to your computer and use it in GitHub Desktop.
Hash with symbol access of Strings in Ruby – the start of an implementation
# encoding: utf-8
class HashWithSymbolAccessOfStrings < Hash
def [](key)
begin
self.fetch key
rescue KeyError
string_key = key.to_s
if string_key != key
self[string_version]
else
self.default(key)
end
end
end
def []=(key, value)
# should work for new keys and overwriting
end
def fetch(*args)
# do I have to override this, too? And all of the other fetching methods?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment