Skip to content

Instantly share code, notes, and snippets.

@AlexNisnevich
Created July 19, 2012 19:51
Show Gist options
  • Save AlexNisnevich/3146343 to your computer and use it in GitHub Desktop.
Save AlexNisnevich/3146343 to your computer and use it in GitHub Desktop.
Playing with Ruby hashes
class Hash
def deep_sort
Hash[sort.map {|k, v| [k, v.is_a?(Hash) ? v.deep_sort : v]}]
end
end
# Hash that allows arbitrarily deep assignments
# e.g.
# h = FreeformHash.new
# h[:a][:b][:c] = 5
class FreeformHash < Hash
def [](key)
unless has_key?(key)
self[key] = FreeformHash.new
end
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment