Skip to content

Instantly share code, notes, and snippets.

@fsword
Created July 6, 2012 07:34
Show Gist options
  • Save fsword/3058698 to your computer and use it in GitHub Desktop.
Save fsword/3058698 to your computer and use it in GitHub Desktop.
tree.rb
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