Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:如何搜索所有文件并忽略一些不需要的文件?_Ruby - Fatal编程技术网

Ruby:如何搜索所有文件并忽略一些不需要的文件?

Ruby:如何搜索所有文件并忽略一些不需要的文件?,ruby,Ruby,我在Ruby目录搜索上遇到问题。我想实现像gitfile这样的东西。 当某些文件列在忽略文件中时,将不会对其进行搜索 比如说 这是一个目录列表,如果我想忽略/bar(及其所有子文件) 如果我使用 Dir[“/****”] 将搜索每个文件。 我怎样才能只搜索foo目录 Dir["/**/*"].reject{|f|f["/bar/"]}.each do |file| puts file end => /foo /foo/c.txt /foo/d.txt 如果有多个sub要过滤,请使用R

我在Ruby目录搜索上遇到问题。我想实现像gitfile这样的东西。 当某些文件列在忽略文件中时,将不会对其进行搜索

比如说 这是一个目录列表,如果我想忽略/bar(及其所有子文件)

如果我使用
Dir[“/****”]
将搜索每个文件。 我怎样才能只搜索foo目录

Dir["/**/*"].reject{|f|f["/bar/"]}.each do |file|
  puts file
end
=>
/foo
/foo/c.txt
/foo/d.txt
如果有多个sub要过滤,请使用Regexp.union,例如所有没有subs\docs和\ruby的我的google驱动器文件

pattern = Regexp.union("C:/Users/peter/Google Drive/ruby", "C:/Users/peter/Google Drive/docs")
Dir["C:/Users/peter/Google Drive/**/**"].reject{|f|f[pattern]}.each do |file|
  p file
end
pattern = Regexp.union("C:/Users/peter/Google Drive/ruby", "C:/Users/peter/Google Drive/docs")
Dir["C:/Users/peter/Google Drive/**/**"].reject{|f|f[pattern]}.each do |file|
  p file
end