Skip to content

Instantly share code, notes, and snippets.

@apk
Created January 28, 2011 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apk/800535 to your computer and use it in GitHub Desktop.
Save apk/800535 to your computer and use it in GitHub Desktop.
Converting find output into a tree of hashes (and a funny use of inject)
tree={}
STDIN.each_line do |l|
a=l.strip.split '/'
a.inject(tree) { |t,n| t[n] ||= {} }
end
puts tree.inspect
def rek t, p
t.each_pair do |k, v|
if (v.empty?)
puts p + "/" + k
else
rek(v, p + "/" + k)
end
end
end
rek tree, ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment