English 中文(简体)
Concurrent modification whilst traversing a ruby Hash
原标题:

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.

最佳回答

As long as no other threads are trying to access the hash, any of those method are fine. If there are any other threads that could access this hash, then none of these approaches are explicitly thread safe. You ll want to make them tread safe, just ensure that your method acquires a lock during the hash modification, and things would work out fine.

Personally I d go with the destructive reject_if! It s cleaner code and accomplishes the same thing without reproducing existing code.

问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签