How to assign hash['a']['b']= 'c' if hash['a'] doesn't exist?

后端 未结 5 1171
渐次进展
渐次进展 2020-11-29 04:46

Is there any way simpler than

if hash.key?(\'a\')
  hash[\'a\'][\'b\'] = \'c\' 
else  
  hash[\'a\'] = {}
  hash[\'a\'][\'b\'] = \'c\' 
end
5条回答
  •  我在风中等你
    2020-11-29 05:19

    The question here: Is auto-initialization of multi-dimensional hash array possible in Ruby, as it is in PHP? provides a very useful AutoHash implementation that does this.

提交回复
热议问题