Suppose you had this:
def wipeProduct(hash, nameToDelete)
hash.each do |i|
key = i[0]
productName = i[1].first
hash.delete(key) if productName==nameToDelete
end
end
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.
I know that you can rewrite this
def wipeProduct(hash, nameToDelete)
keysToDelete = []
hash.each do |i|
key = i[0]
productName = i[1].first
keysToDelete << key if productName==nameToDelete
end
keysToDelete.each {|key| hash.delete(key) }
end
or even:
def wipeProduct(hash, nameToDelete)
hash.reject!{|key,value| nameToDelete==value.first}
end
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)
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.