Created
November 10, 2011 15:14
-
-
Save slhck/1355082 to your computer and use it in GitHub Desktop.
Recursively build a hash for directory listing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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