Skip to content

Instantly share code, notes, and snippets.

@vikashvikram
Created December 14, 2015 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikashvikram/ab031cfe653d1aeb9a56 to your computer and use it in GitHub Desktop.
Save vikashvikram/ab031cfe653d1aeb9a56 to your computer and use it in GitHub Desktop.
# 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