New trail: Ruby Challenges

I started this ruby challenge but I feel like I went about the wrong way to approach this. This is the first exercise to analyze Macbeth. So I used nokogiri gem to parse the xml.

@doc = Nokogiri::HTML(open("http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml"))

Then I took out all the speech and return them as array.
all_speeches = @doc.xpath("//speech").remove.to_a

From the array of arrays, I made an array of hashes for each speaker and their lines.count:
a = all_speeches.map{ |s| { s.css("speaker/text()").to_s => s.css("line").count } }

This is where I went to stackoverflow for help. I want to combine values of hashes with the same key.
Stackoverflow suggested:
cache = Hash.new { |h, k| h[k] = { k => 0 } }
puts a.flat_map(&:to_a).each_with_object(cache) { |(k,v),h| h[k][k] += v }.values

Those two lines did the trick, but I am having a hard time understanding what is going on. especially h[k][k] in the block. I know a new hash was created called cache, and it is passed into each_with_object(). I don’t know if this is the best way to go about solving this exercise. For me this is crazy ruby, haha. Any pointers will help. Thanks in advance!