hash = Hash.new(Hash.new([]))
hash[1][2] << 3
hash[1][2] # => [3]
hash # => {}
hash.keys # => []
hash.values # => []
What’s going on? Ruby’s hiding data (1.9.3p125)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Ruby hides neither data nor its docs.
Default value you pass into the
Hashconstructor is returned whenever the key is not found in the hash. But this default value is never actually stored into the hash on its own.To get what you want you should use
Hashconstructor with block and store default value into the hash yourself (on both levels of your nested hash):