将1个哈希的内容添加到另一个

时间:2018-07-12 14:19:16

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个更改的父哈希,我想确保子哈希进行这些更改,但还要保留以前拥有的密钥,并且这些密钥不应该丢失

这些是我拥有的示例哈希

one = {"endpoints"=>["get_route"], "features"=>["channel_selection"], "get_route"=>{"output"=>["total_length", "seca_length"], "options"=>["panama", "abcd"]}}

other = {"endpoints"=>["get_route"], "features"=>["channel_selection"], "get_route"=>{"output"=>["total_length", "seca_length"], "options"=>["panama", "suez", "kiel"]}}

我希望其他哈希现在看起来像

other = {"endpoints"=>["get_route"], "features"=>["channel_selection"], "get_route"=>{"output"=>["total_length", "seca_length"], "options"=>["panama", "abcd", suez", "kiel"]}}

我尝试了以下代码,但无法正常工作

result = propogate_changes(one, other)

def propogate_changes(one, other)
    one_keys = one.keys
    other_keys = other.keys
    combined = Hash.new
    unique_keys = one_keys.concat(other_keys).uniq

    unique_keys.each do |key|
        if(one[key].is_a?(Array)) then
            # if(other[key] == nil) then
            #     combined[key] = one[key]
            # else
                combined[key] = one[key].concat(other[key]).uniq
            # end
        else
            combined[key] = add_allowance(one[key], other[key])
        end
    end
    return combined
end

当一个键存在而另一个键不存在时,以上代码将失败

我也尝试过merge, deep_merge, reverse_merge,但是它们都用一个哈希值覆盖了我的另一个哈希值,但是没有一个保留原始数据。

任何对此的建议将不胜感激

1 个答案:

答案 0 :(得分:1)

尝试使用此自定义合并逻辑。

def find_missing_items_in_arr(arr1, arr2)
  arr1_size = arr1.size
  arr2_size = arr2.size

  if (arr1_size == arr2_size) && (arr1 & arr2).size == arr1_size
    return [] # Same array
  end

  arr2 - arr1
end

def custom_merge(target_hash, source_hash)
  # If you want to preserve frozen state of entries, please use `clone`
  duped_target_hash = target_hash.dup

  source_hash.each do |k, v|
    unless duped_target_hash.key?(k)
      duped_target_hash[k] = v
      next
    end

    case v
      when Array
        missing_items_in_arr = find_missing_items_in_arr(duped_target_hash[k], v)
        if missing_items_in_arr.size > 0
          duped_target_hash[k] += missing_items_in_arr
        end
      when Hash
        duped_target_hash[k] = custom_merge(duped_target_hash[k], v)
      else
        # Nothing to do here
    end
  end

  duped_target_hash
end

用法

one = {
  "endpoints"=>["get_route"],
  "features"=>["channel_selection"],
  "get_route"=> {
    "output"=> ["total_length", "seca_length"],
    "options"=> ["panama", "abcd"]
  }
}

other = {
  "endpoints"=>["get_route"],
  "features"=>["channel_selection"],
  "get_route"=> {
    "output"=> ["total_length", "seca_length"],
    "options"=> ["panama", "suez", "kiel"]
  }
}

rs_hash = custom_merge(other, one)

puts rs_hash

注意:Rails提供了deep_merge,但可以在Rails外部使用。我已经测试过,它会返回您想要的输出。它还处理更多的嵌套条目,例如

one = {
  "endpoints"=>["get_route"],
  "features"=>["channel_selection"],
  "get_route"=> {
    "output"=> ["total_length", "seca_length"],
    "options"=> ["panama", "abcd"],

    "custom_options" => {
      "custom_output" => ["abc"],
      "custom_input" => ["xyz" ]
    }
  }
}

other = {
  "endpoints"=>["get_route"],
  "features"=>["channel_selection"],
  "get_route"=> {
    "output"=> ["total_length", "seca_length"],
    "options"=> ["panama", "suez", "kiel"],

    "custom_options" => {
      "custom_output" => ["abc", "def"]
    }
  }
}

希望这会有所帮助。