有没有比Ruby的Dir.glob更快的替代品?

时间:2022-09-01 23:25:04

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

我正在使用Dir.glob来访问与通配符模式匹配的所有文件集。

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.

因为这个glob调用是递归的,并且因为涉及大量文件,所以在块启动之前,glob需要很长时间来构建文件数组。

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.

我想要的是一种访问所有相同文件的方法,但是在Ruby“发现”每个文件之后立即调用该块,以便立即处理第一个文件而不是在等待整个目录树完成搜索之后。

Is there such a construction?

有这样的建筑吗?

1 个解决方案

#1


4  

It seems no built-in way can do this.

似乎没有内置的方法可以做到这一点。

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

希望这可以帮到你。通过递归扩展模式来查找文件(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}

#1


4  

It seems no built-in way can do this.

似乎没有内置的方法可以做到这一点。

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

希望这可以帮到你。通过递归扩展模式来查找文件(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}