如何在多维哈希中添加一行?

时间:2011-10-03 15:48:44

标签: ruby hash hash-of-hashes

我对ruby很新,我在这个简单的问题上阻止了:

我有以下哈希:

theData"=>{"586"=>{"status"=>"0"},
           "585"=>{"status"=>"0"}}

我想在每个级别添加一行“current_editor”,以获得以下哈希:

theData"=>{"586"=>{"status"=>"0", "current_editor" => "3"},
           "585"=>{"status"=>"0", "current_editor" => "3"}}

我该怎么做?非常感谢提前!

1 个答案:

答案 0 :(得分:1)

theData = {"586"=>{"status"=>"0"}, "585"=>{"status"=>"0"}}
theData.each{|k, v| theData[k]["current_editor"] = 3}
#=> {"586"=>{"status"=>"0", "current_editor"=>3}, 
#=>  "585"=>{"status"=>"0", "current_editor"=>3}} 
相关问题