少しだけシンプルになったと思う。
locate.rb
#!ruby -Ku
module Locate
EXCLUDE_PATTERN = /^(\.|\.\.|\.svn)$/i
module_function
def locate(dirname, patterns)
each_entries(dirname) do |pathname, filename|
if File.directory?(pathname)
puts(pathname + '/') if match?(filename, patterns)
locate(pathname, patterns)
else
puts(pathname) if match?(filename, patterns)
end
end
end
def each_entries(dirname)
entries(dirname).each do |filename|
pathname = (dirname == '.' ? filename : File.join(dirname, filename))
yield(pathname, filename)
end
end
def entries(pathname)
Dir.entries(pathname).select do |filename|
filename !~ EXCLUDE_PATTERN
end
end
def match?(filename, patterns)
patterns.each do |pattern|
return true if filename =~ pattern
end
return false
end
end
if $0 == __FILE__
Locate.locate('.', ARGV.map{|pattern|Regexp.new(pattern, Regexp::IGNORECASE)})
end
0 件のコメント:
コメントを投稿