Created
September 3, 2014 17:52
-
-
Save probablykabari/d39146a0bb556d411442 to your computer and use it in GitHub Desktop.
Quick look at hash nesting
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 = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) } | |
hash["down"]["in-the"]["basement"] = "hello?" | |
hash | |
# => {"down"=>{"in-the"=>{"basement"=>"hello?"}}} | |
hash.has_key?("i-do-not") | |
# => false | |
hash.default = lambda{|h,k| h[k] = "bankai!" } # no longer a deeply nested/fun hash | |
hash["not"]["nested"] = ":(" | |
# NoMethodError: undefined method `[]=' for #<Proc:0x00000101b44888@(irb):69 (lambda)> | |
hash["dang"] = "I get it" | |
hash | |
# => {"down"=>{"in-the"=>{"basement"=>"hello?"}}, "dang"=>"I get it"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe add
Hash#nested
tolib
that makes a blank new hash with this functionality?