ruby add new key-value pair to nested hash

Viewed 12264

Suppose we have a nested hash here.

a = {:"0" => {:CA => {:count => 10}}}

if we want to add a new hash pair to that hash, say

a = {:"0" => {:NY => {:count => 11}}} 

and let it become

a = {:"0" => {:CA => {:count =>10}, :NY => {:count => 11}}}

what should I do?

I've tried

a[:0][:NY][:count] = 11

but get the error "undefined method `[]=' for nil:NilClass (NoMethodError)"

2 Answers
Related