Skip to content

Instantly share code, notes, and snippets.

@bagwanpankaj
Created January 29, 2011 06:38
Show Gist options
  • Save bagwanpankaj/801613 to your computer and use it in GitHub Desktop.
Save bagwanpankaj/801613 to your computer and use it in GitHub Desktop.
default_proc for Hash
hs = Hash.new
hs[:absent_key]
# => {}
hs[:absent_key][:some_other_key]
# NoMethodError: undefined method `[]' for nil:NilClass
hs = Hash.new("not present")
hs[:absent_key]
# => "not present"
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