我正在尝试创建一个嵌套哈希,其中内部值为数组。例如
{"monday"=>{"morning"=>["John", "Katie", "Dave"],"afternoon"=>["Anne", "Charlie"]},
"tuesday"=>{"morning"=>["Joe"],"afternoon"=>["Chris","Tim","Melissa"]}}
Run Code Online (Sandbox Code Playgroud)
我试过
h = Hash.new( |hash, key| hash[key] = Hash.new([]) }
Run Code Online (Sandbox Code Playgroud)
当我尝试
h["monday"]["morning"].append("Ben")
Run Code Online (Sandbox Code Playgroud)
看看 h,我明白了
{"monday" => {}}
Run Code Online (Sandbox Code Playgroud)
而不是
{"monday" => {"morning"=>["Ben"]}}
Run Code Online (Sandbox Code Playgroud)
我对 Ruby 还很陌生,有什么关于获得我想要的功能的建议吗?
ruby ×1