Created
December 14, 2015 14:41
-
-
Save vikashvikram/ab031cfe653d1aeb9a56 to your computer and use it in GitHub Desktop.
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
# FrankensteinHash is a subclass of Hash | |
# It lets you create many levels of keys in Hash at one go. | |
class FrankensteinHash < Hash | |
def [](key) | |
fetch(key) {|el| self[el] = FrankensteinHash.new} | |
end | |
end | |
h = FrankensteinHash.new | |
h["a"]["b"]["c"] = 1 #=> 1 | |
h #=> {"a"=>{"b"=>{"c"=>1}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment