lib/find.rb


DEFINITIONS

This source file includes following functions.


   1  # Usage:
   2  #       require "find"
   3  #
   4  #       Find.find('/foo','/bar') {|f| ...}
   5  #  or
   6  #       include Find
   7  #       find('/foo','/bar') {|f| ...}
   8  #
   9  
  10  module Find
  11    def find(*path)
  12      path.collect!{|d| d.dup}
  13      while file = path.shift
  14        catch(:prune) do
  15          yield file
  16          begin
  17            if File.lstat(file).directory? then
  18              d = Dir.open(file)
  19              begin
  20                for f in d
  21                  next if f == "." or f == ".."
  22                  if File::ALT_SEPARATOR and file =~ /^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
  23                    f = file + f
  24                  elsif file == "/" then
  25                    f = "/" + f
  26                  else
  27                    f = File.join(file, f)
  28                  end
  29                  path.unshift f
  30                end
  31              ensure
  32                d.close
  33              end
  34            end
  35         rescue Errno::ENOENT, Errno::EACCES
  36          end
  37        end
  38      end
  39    end
  40  
  41    def prune
  42      throw :prune
  43    end
  44    module_function :find, :prune
  45  end