From: Brian Candler Date: 2010-03-09T22:25:20+09:00 Subject: Re: Flatten out Hash Perhaps a better solution is to yield an array of keys (being the 'path' to reach the end node), because then the caller can choose how to combine them. class Hash def flat_each(prefix=[], &blk) each do |k,v| if v.is_a?(Hash) v.flat_each(prefix+[k], &blk) else yield prefix+[k], v end end end end require 'enumerator' h = {"a"=>{"b"=>{"c"=>1}, "b2"=>{"c2"=>2}}} h = {"a"=>{"b"=>{"c"=>1}, "b2"=>{"c2"=> {"d2" => 2}}}} p h.to_enum(:flat_each).collect { |k,v| [k.join(": "),v] } h.flat_each do |k,v| puts "#{k.join("/")} => #{v}" end -- Posted via http://www.ruby-forum.com/.