Created
January 29, 2011 06:38
-
-
Save bagwanpankaj/801613 to your computer and use it in GitHub Desktop.
default_proc for Hash
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
hs = Hash.new | |
hs[:absent_key] | |
# => {} | |
hs[:absent_key][:some_other_key] | |
# NoMethodError: undefined method `[]' for nil:NilClass |
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
hs = Hash.new("not present") | |
hs[:absent_key] | |
# => "not present" |
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
hs = Hash.new{|k,v| Hash.new(&k.default_proc)} | |
hs[:absent_key][:another_key] | |
#=> {} | |
hs[:absent_key][:another_key][:b] | |
#=> {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment