Q:

RUBY: Combining 2 different arrays with duplicate values into hash

I have 2 Arrays.
product_name = ["Pomegranate", "Raspberry", "Miracle fruit", "Raspberry"]
product_quantity =  [2, 4, 5, 5]

I'd like to know how to initialize a hash such that it becomes
product_hash = {"Pomegranate"=>2, "Raspberry"=>9, "Miracle fruit"=>5}

A:

Use each_with_object: 
product_name.zip(product_quantity)
            .each_with_object({}) {|(k, v), h| h[k] ? h[k] += v : h[k] = v }
#=> {"Pomegranate"=>2, "Raspberry"=>9, "Miracle fruit"=>5}

Or just use hash with default value:
product_name.zip(product_quantity)
            .each_with_object(Hash.new(0)) {|(k, v), h| h[k] += v }
#=> {"Pomegranate"=>2, "Raspberry"=>9, "Miracle fruit"=>5}

A:

I would start with something like this:
product_name.zip(product_quantity)
            .group_by(&:first)
            .map { |k, v| [k, v.map(&:last).inject(:+)] }
            .to_h
#=> { "Pomegranate" => 2, "Raspberry" => 9, "Miracle fruit" => 5}

I suggest to lookup each method in the Ruby's docs for Array and Hash and to check in the console what each the  intermediate step returns.

