Merging multi-dimensional hashes in Ruby

前端 未结 6 2128
轮回少年
轮回少年 2020-12-07 02:38

I have two hashes which have a structure something similar to this:

hash_a = { :a => { :b => { :c => \"d\" } } }
hash_b = { :a => { :b => { :x         


        
6条回答
  •  感情败类
    2020-12-07 02:57

    In order to merge one into the other as the ticket suggested, you could modify @Phrogz function

    def recurse_merge( merge_from, merge_to )
      merge_from.merge(merge_to) do |_,x,y|
        (x.is_a?(Hash) && y.is_a?(Hash)) ? recurse_merge(x,y) : x
      end
    end
    

    In case there is duplicate key, it will only use the content of merge_from hash

提交回复
热议问题