Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a faster alternative to Ruby's Dir.glob?

I'm using Dir.glob to visit the set of all files matching a wildcard pattern.

Dir.glob( '**/*.txt' ) { |file_name|
    parse file_name
}

Because this glob call is recursive and because lots of files are involved, glob takes a long time to build the array of files before the block starts.

What I want instead is a way of visiting all of the same files, but calling the block immediately after Ruby "discovers" each file, so that the first file is processed right away rather than after waiting for the whole directory tree to finish being searched.

Is there such a construction?

like image 609
OldPeculier Avatar asked May 10 '13 16:05

OldPeculier


2 Answers

You can also use find and IO.popen

IO.popen("find . -name '*.txt'").each { |path| parse(path.chomp) }
like image 154
ZogStriP Avatar answered Nov 11 '22 09:11

ZogStriP


It seems no built-in way can do this.

Hope this may help you. Find files by expanding pattern recursively (Ruby 1.9.3):

class Dir
   def self.glob_recursively( pattern, &block )
     begin
       glob(pattern, &block)
       dirs = glob('*').select { |f| File.directory? f }
       dirs.each do |dir|
         # Do not process symlink
         next if File.symlink? dir
         chdir dir
         glob_recursively(pattern, &block)
         chdir '..'
       end
     rescue SystemCallError => e
       # STDERR
       warn "ERROR: #{pwd} - #{e}"
     end
   end
end

Dir.glob_recursively ('*.txt') {|file| puts file}
like image 36
zhongguoa Avatar answered Nov 11 '22 09:11

zhongguoa