ruby 2.5
我有以下代码:
test = {'primer' => 'grey'}
layers = ["tan","burgundy"]
fillers = ["blue","yellow"]
layers.each do |l|
fillers.each do |f|
test[l] = {} if !test.respond_to?(l)
test[l][f] = {} if !test[l].respond_to?(f)
end
end
当我在 irb 中运行它时,我得到以下信息:
{"primer"=>"grey", "tan"=>{"yellow"=>{}}, "burgundy"=>{"yellow"=>{}}}
我期待:
{"primer"=>"grey", "tan"=>{"blue"=>{},"yellow"=>{}}, "burgundy"=>{"blue"=>{},"yellow"=>{}}}
为什么第一个 respond_to 产生密钥,而第二个则替换前一个密钥?
我错过了什么?