Created
July 6, 2012 07:34
-
-
Save fsword/3058698 to your computer and use it in GitHub Desktop.
tree.rb
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
module MyTool | |
module Tree | |
class << self | |
def loop root, &block | |
return false unless File.exist? root | |
block_given? ? exec(root, &block) : collect(root) | |
end | |
def exec root, &block | |
if File.directory? root | |
Dir.entries(root)[2..-1].each do |f| | |
exec File.expand_path(f,root), &block | |
end | |
else | |
block.call root | |
end | |
end | |
def collect root, list=[] | |
if File.directory? root | |
Dir.entries(root)[2..-1].each do |f| | |
new_path = File.expand_path(f,root) | |
if File.file? new_path | |
list << new_path | |
else | |
list = collect new_path, list | |
end | |
end | |
else | |
list << root | |
end | |
list | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment