Rails速度很快

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)

即展平端阵列.结构不会比这里显示的任何"更深".

单行代码是首选.

Mar*_*pka 7

这应该工作:

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)