Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created January 9, 2009 16:36
Show Gist options
  • Save jcoglan/45161 to your computer and use it in GitHub Desktop.
Save jcoglan/45161 to your computer and use it in GitHub Desktop.
# Man, I hate having to sort out strings vs symbols in Hashes
class AgnosticHash < Hash
def [](key)
super(coerce_key(key))
end
def []=(key, value)
super(coerce_key(key), value)
end
def to_h
Hash[self]
end
private
def coerce_key(key)
[key, key.to_s, key.to_sym].find { |k| has_key?(k) } || key.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment