Skip to content

Instantly share code, notes, and snippets.

@ifukazoo
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save ifukazoo/10560410 to your computer and use it in GitHub Desktop.

Select an option

Save ifukazoo/10560410 to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby -w
# encoding: utf-8
#
def get_info(path)
Info.new(path)
end
class Info
def initialize(path)
@path = path
@stat = File::Stat.new(path)
end
attr_reader :path, :stat
def <=>(op)
self.stat <=> op.stat
end
def directory?
@stat.directory?
end
end
def get_useful_contents(path)
Dir.entries(path).reject {|e| ['.', '..'].include?(e)}
end
def travarse(path, &order)
names = get_useful_contents(path)
contents = names.map { |e| path + '/' + e }.map { |e| get_info(e) }
contents = contents.sort(&order).map do |e|
if (e.directory? && e.path != path)
e = travarse(e.path, &order)
else
e = [e]
end
end
contents.flatten
end
dir = ARGV.shift
fail if dir.nil?
travarse(dir) {|a, b| a <=>b}.each do |e|
p e.path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment