开发者

How to add a line to multi dimensional hash?

开发者 https://www.devze.com 2023-04-10 01:00 出处:网络
I\'m quite new to ruby and I\'m blocking on this simple issue: I have the following hash: 开发者_C百科theData\"=>{\"586\"=>{\"status\"=>\"0\"},

I'm quite new to ruby and I'm blocking on this simple issue:

I have the following hash:

开发者_C百科
theData"=>{"586"=>{"status"=>"0"},
           "585"=>{"status"=>"0"}}

I would like to add a line "current_editor" at each level, to get the following hash:

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

How can I do this? Many thanks in advance!


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}} 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号