From: Dave Thomas Date: 2001-09-04T16:37:26+09:00 Subject: [ruby-talk:20841] Re: modify hash during iteration Tomas Brixi writes: > Hello > > I have a question to following piece of code > ----------------- > h = Hash.new > #assign some data > > h.each do |pair| > h[pair[0]] = new_value > end > ----------------- > > is this the right way or are there better ways to do this I'm not sure what you're asking about. If it's the pair[0] call, then you could do h = { "cat" => 1, "dog" => 99 } h.each do |key, | h[key] += 1 end If instead you're asking about what happens if you modify the keyset of the hash, not the values, your iterations may not cover the entire original hash: h = { 1 => :one, 2 => :two, 3 => :three } h.each do |key, value| puts "#{key} => #{value}" h.delete key+1 end #=> 1 => one 3 => three Regards Dave