如果hash ['a']不存在,如何分配hash ['a'] ['b'] ='c'?

问题描述 投票:20回答:5

有什么比这简单的方法

if hash.key?('a')
  hash['a']['b'] = 'c' 
else  
  hash['a'] = {}
  hash['a']['b'] = 'c' 
end
ruby hash variable-assignment autovivification hash-of-hashes
5个回答
© www.soinside.com 2019 - 2024. All rights reserved.