Converting a hash into a nested hash

后端 未结 6 1734
后悔当初
后悔当初 2020-12-11 07:20

This question is the inverse of this question.

Given a hash that has an array for each key like

{
    [:a, :b, :c] => 1,
    [:a, :b, :d] => 2,         


        
6条回答
  •  余生分开走
    2020-12-11 07:48

    Using DeepEnumerable:

    require DeepEnumerable
    
    h = {[:a, :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4}
    
    h.inject({}){|hash, kv| hash.deep_set(*kv)}
    

提交回复
热议问题