title: Concurrent modification whilst traversing a ruby Hash
body: <p>Suppose you had this:</p>
  
  <pre><code>def wipeProduct(hash, nameToDelete) 
    hash.each do |i| 
      key = i[0]
      productName = i[1].first
      hash.delete(key) if productName==nameToDelete
    end
  end
  </code></pre>
  
  <p>I'm not sure it's safe to delete things from a hash whilst you're iterating over the key-value pairs of the hash. I checked the RI documentation but I haven't seen anything about concurrent modification on Hash.</p>
  
  <p>I know that you can rewrite this</p>
  
  <pre><code>def wipeProduct(hash, nameToDelete) 
    keysToDelete = []
    hash.each do |i| 
      key = i[0]
      productName = i[1].first
      keysToDelete &lt;&lt; key if productName==nameToDelete
    end
    keysToDelete.each {|key| hash.delete(key) }
  end
  </code></pre>
  
  <p>or even:</p>
  
  <pre><code>def wipeProduct(hash, nameToDelete)
    hash.reject!{|key,value| nameToDelete==value.first}
  end
  </code></pre>
  
  <p>but I want to know if concurrent modification as shown in the first example above is a problem? (It doesn't seem to be when I run the code)</p>
  
  <p><strong>Question: Does anyone know what the position on concurrent modification is with Hash? (... and the other collections?) - and I'd certainly like any links to resources documenting this if you would please.</strong></p>