문제

I'm loading the configuration from a YAML file.

I have a hash that looks like this: {:first=>{:abc=>[["one", "two", "three"]], :def => [["one", "two", "three"]]}, :second=>{:abc=>[["one", "two", "three"]]}}

But I would like to get:

{:first=>{:abc=>["one", "two", "three"], :def => ["one", "two", "three"]}, :second=>{:abc=>["one", "two", "three"]}}

I.e flatten the end arrays. Structure is not going to be any "deeper" than displayed here.

One-liner code is preferred.

도움이 되었습니까?

해결책

This should work:

hash.each_value do |nested_hash|
  nested_hash.each_value do |array|
    array.flatten!
  end
end

or, in one-liner version:

hash.each_value { |nested_hash| nested_hash.each_value(&:flatten!) }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top