From: Trans Date: 2008-11-05T16:39:48+09:00 Subject: Re: replace values in hash On Nov 5, 1:53 am, Jason Lillywhite wrote: > how would I iterate over a hash, such as > > x = { 'a' => "hi", 'b' => nil, 'c' => "do"} x.each{ |k,v| x[k] = 'foo' unless v } However, it is not always sane to alter something while you are iterating over it. So, h = {} x.each{ |k,v| h[k] = v.nil? ? v : 'foo' } x.replace(h) Or use Facets Enumerable#mash (alias #graph) require 'facets/enumerable/mash' x = x.mash{ |k,v| [k, v.nil? ? v : 'foo'] } http://facets.rubyforge.org/doc/api/core/classes/Enumerable.html#M000429 7.