Skip to content

Instantly share code, notes, and snippets.

@contribu
Created August 3, 2016 12:51
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 contribu/ee2bcad0dcd002b8770f7047bc5355e3 to your computer and use it in GitHub Desktop.
Save contribu/ee2bcad0dcd002b8770f7047bc5355e3 to your computer and use it in GitHub Desktop.
new_safe_hash.rb (multidimensional hash)
def new_safe_hash()
ifnone = lambda { |hash, key|
hash[key] = Hash.new(&ifnone)
}
Hash.new(&ifnone)
end
a = new_safe_hash()
a['x']['y'] = 3
puts a
# {"x"=>{"y"=>3}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment