Skip to content

Instantly share code, notes, and snippets.

@slhck
Created November 10, 2011 15:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slhck/1355082 to your computer and use it in GitHub Desktop.
Save slhck/1355082 to your computer and use it in GitHub Desktop.
Recursively build a hash for directory listing
#!/usr/bin/env ruby
# Recursively build a hash for directory listing
def create_hash(path, name = nil)
data = {:parent => (name || path)}
data[:children] = children = []
Dir.foreach(@path) do |entry|
next if entry == '..' or entry == '.'
full_path = File.join(path, entry)
if File.directory?(full_path)
children << create_hash(full_path, entry)
else
children << entry
end
end
return data
end
@path = ARGV[0]
create_hash(@path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment