我不确定我是否正确理解了这项任务,但是我已经写了: "为工作人员数组中的每个工作人员添加一个名为" season"并为其分配值" winter""
hash1 = {
:title => "MegaCorporation",
:location => "Europe",
:supervisors => [
{:name=>"Bill", :about=>"blah blah blah 1" },
{:name=>"John", :about=>"blah blah blah 2"},
{:name=>"Tiffany", :about=>"blah blah blah 3"}
],
:workers => [
{:name => "Alex", :level => "A"},
{:name=>"Anna", :level => "B"},
{:name => "Ashley", :level => "C"},
{:name => "Mike", :level => "B"}
]
}
所以我的代码是:
hash1[:workers].each { |key,value| key["season"] = "winter"}
我做的是我的问题是正确的还是什么? :)
答案 0 :(得分:2)
就我个人而言,我会这样做:
hash1[:workers].each { |worker| worker[:season] = "winter"}
阅读是有道理的,并且不会将责任推迟到您可能不理解的其他功能。
答案 1 :(得分:1)
hash1[:workers].each { |h| h.merge!(season: 'winter') }