Dhaval Chheda
Dhaval Chheda

Reputation: 5147

add contents of 1 hash into another

I have a parent hash which changes and I want to ensure that the child hashes take these changes but also retain keys that they had before and those should not be lost

These are the sample hashes that I have

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"]}}

I want the other hash to now look like

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

I have tried the following code but it is not working

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

The above code fails when a key is present in one but missing in another

I also tried merge, deep_merge, reverse_merge but they all overwrite my other hash with one hash but none of them retain the original data.

Any advise on this will be appreciated

Upvotes: 1

Views: 191

Answers (1)

Jignesh Gohel
Jignesh Gohel

Reputation: 6552

Try this custom merge logic.

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

Usage

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

Note: Rails provides a deep_merge but this can be used outside Rails. I have tested and it returns your desired output. Also it handles more nested entries like

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"]
    }
  }
}

Hope this helps.

Upvotes: 1

Related Questions