Find every path and it's value in a Hash
This method takes a block as argument which is called each time a the recursivly searched Hash returns a key that does not point to another Hash.
Example:
paths = [] complex_hash = Hash[ :a => { :aa => '1', :ab => '2' }, :b => { :ba => '3', :bb => '4' } ] complex_hash.each_path { |path, value| paths << [ path, value ] } paths.inspect # => "[[\"b/ba/\", \"3\"], [\"b/bb/\", \"4\"], [\"a/aa/\", \"1\"], [\"a/ab/\", \"2\"]]"
class Hash def each_path raise ArgumentError unless block_given? self.class.each_path( self ) { |path, object| yield path, object } end protected def self.each_path( object, path = '', &block ) if object.is_a?( Hash ) then object.each do |key, value| self.each_path value, "#{ path }#{ key }/", &block end else yield path, object end end end