Ruby:如何搜索所有文件并忽略一些不需要的文件?
我在Ruby目录搜索上遇到问题。我想实现像gitfile这样的东西。 当某些文件列在忽略文件中时,将不会对其进行搜索 比如说 这是一个目录列表,如果我想忽略/bar(及其所有子文件) 如果我使用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
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