From: Trans Date: 2005-04-13T09:04:36+09:00 Subject: Re: help traversing and modifying hash key and value inplace So here's what I've settled on. Good? Or is there still a better way to do? # Returns a new hash created by traversing the hash and its subhashes, # executing the given block on the key and value. The block should # return a 2-element array of the form +[key, value]+. # # h = { "A"=>"A", "B"=>"B" } # h.traverse { |k,v| [k.downcase, v] } # h #=> { "a"=>"A", "b"=>"B" } # def traverse(&b) inject({}) do |h,(k,v)| nk, nv = b[k,v] h[nk] = (Hash === nv ? nv.traverse(&b) : nv) h end end