我真的不知道标题是否正确,但问题很简单:
我有一个值和一个键。
密钥如下:
"one.two.three"
现在,我如何设置这个散列:
params['one']['two']['three'] = value
发布于 2012-01-26 20:04:16
您可以使用inject方法:
key = "one.two.three"
value = 5
arr = key.split(".").reverse
arr[1..-1].inject({arr[0] => value}){ |memo, i| {i => memo} }
# => {"one"=>{"two"=>{"three"=>5}}}https://stackoverflow.com/questions/9015977
复制相似问题