title: How to merge two arrays of hashes
body: <p>I have two arrays of hashes:</p>
  
  <pre><code>a = [
    {
      key: 1,
      value: "foo"
    },
    {
      key: 2,
      value: "baz"
    }
  ]
  
  b = [
    {
      key: 1,
      value: "bar"
    },
    {
      key: 1000,
      value: "something"
    }
  ]
  </code></pre>
  
  <p>I want to merge them into one array of hashes, so essentially <code>a + b</code> except I want any duplicated key in <code>b</code> to overwrite those in <code>a</code>. In this case, both <code>a</code> and <code>b</code> contain a key <code>1</code> and I want the final result to have <code>b</code>'s key value pair.</p>
  
  <p>Here's the expected result:</p>
  
  <pre><code>expected = [
    {
      key: 1,
      value: "bar"
    },
    {
      key: 2,
      value: "baz"
    },
    {
      key: 1000,
      value: "something"
    }
  ]
  </code></pre>
  
  <p>I got it to work but I was wondering if there's a less wordy way of doing this:</p>
  
  <pre><code>hash_result = {}
  a.each do |item|
    hash_result[item[:key]] = item[:value]
  end
  
  b.each do |item|
    hash_result[item[:key]] = item[:value]
  end
  
  result = []
  hash_result.each do |k,v|
    result &lt;&lt; {:key =&gt; k, :value =&gt; v}
  end
  
  puts result
  
  puts expected == result # prints true
  </code></pre>