Created
July 19, 2012 19:51
-
-
Save AlexNisnevich/3146343 to your computer and use it in GitHub Desktop.
Playing with Ruby hashes
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
class Hash | |
def deep_sort | |
Hash[sort.map {|k, v| [k, v.is_a?(Hash) ? v.deep_sort : v]}] | |
end | |
end |
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
# 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