From: Bill Kelly Date: 2011-01-21T21:22:01+09:00 Subject: [ruby-core:34733] Re: [Ruby 1.8-Feature#4239] Let's begin a talk for "1.8.8" -- How's needed for surviving 1.8? Zeno, Zeno Davatz wrote: > > Do you use Hash-Interations? I don't think so. It does not > seems so or you just started using them in Ruby 1.9.2. We > use them at least since Ruby 1.8.6. Since you ask: I've been using ruby on a roughly daily basis since 1999. Here is code from a current project where I take steps to avoid a Hash iterator due to the potential for the Hash to be modified during iteration: def close_all_catalogs # NOTE: sv.close_all_catalogs may switch fibers, # and we don't want to be iterating on @servers itself during that # time, else another method like add_server would get # "RuntimeError - can't add a new key into hash during iteration" svs = @servers.values svs.each do |sv| sv.close_all_catalogs end end > Also I do not play the Lottery. I prefer consistency. You *are* playing the Lottery in ruby 1.8 if you are modifying a hash during iteration. $ ruby186 -v -e "h = {:a=>123,:b=>456,:c=>789}; h.each_key {|k| p k; 3.times {|i| h[i.to_s]=i}}" ruby 1.8.6 (2009-06-08 patchlevel 369) [i386-mswin32_71] :a :b :c "0" "1" "2" $ ruby186 -v -e "h = {:a=>123,:b=>456,:c=>789}; h.each_key {|k| p k; 10000.times {|i| h[i.to_s]=i}}" ruby 1.8.6 (2009-06-08 patchlevel 369) [i386-mswin32_71] :a -e:1:in `each_key': hash modified during iteration (RuntimeError) from -e:1 You should *not* be adding new keys to a Hash during iteration, if you claim to value "consistency". Regards, Bill