From: ts Date: 2001-09-04T17:04:15+09:00 Subject: [ruby-talk:20843] Re: modify hash during iteration >>>>> "T" == Tomas Brixi writes: T> The question was about modifying the value of the key (pair[0]) Don't modify the key otherwise you'll have result difficult to predict. Here a small example pigeon% cat b.rb #!/usr/bin/ruby h = {} ('a' .. 'i').each {|k| h[k] = k} h.each do |k, v| p "key : #{k}" h["a" + k] = v end p h h = {} ('a' .. 'z').each {|k| h[k] = k} h.each do |k, v| h["a" + k] = v end pigeon% pigeon% b.rb "key : e" "key : f" "key : g" "key : h" "key : i" "key : a" "key : b" "key : ae" "key : c" "key : af" "key : d" {"ag"=>"g", "e"=>"e", "ah"=>"h", "f"=>"f", "aa"=>"a", "ai"=>"i", "g"=>"g", "ab"=>"b", "h"=>"h", "ac"=>"c", "i"=>"i", "a"=>"a", "ad"=>"d", "b"=>"b", "aae"=>"e", "ae"=>"e", "c"=>"c", "aaf"=>"f", "af"=>"f", "d"=>"d"} ./b.rb:12:in `each': rehash occurred during iteration (IndexError) from ./b.rb:11 pigeon% Guy Decoux