Dmi*_*tri 0 ruby arrays hash ruby-on-rails flatten
我正在从YAML文件加载配置.
我有一个看起来像这样的哈希: {:first=>{:abc=>[["one", "two", "three"]], :def => [["one", "two", "three"]]}, :second=>{:abc=>[["one", "two", "three"]]}}
但我想得到:
{:first=>{:abc=>["one", "two", "three"], :def => ["one", "two", "three"]}, :second=>{:abc=>["one", "two", "three"]}}
Run Code Online (Sandbox Code Playgroud)
即展平端阵列.结构不会比这里显示的任何"更深".
单行代码是首选.
这应该工作:
hash.each_value do |nested_hash|
nested_hash.each_value do |array|
array.flatten!
end
end
Run Code Online (Sandbox Code Playgroud)
或者,在单行版本中:
hash.each_value { |nested_hash| nested_hash.each_value(&:flatten!) }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |