bash 有没有办法在 Ruby 中 glob 一个目录但排除某些目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4505566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to glob a directory in Ruby but exclude certain directories?
提问by jarjar
I want to glob a directory to post-process header files. Yet I want to exclude some directories in the project. Right now the default way is...
我想将一个目录全局化到后处理头文件。但是我想排除项目中的一些目录。现在默认的方式是...
Dir["**/*.h"].each { |header|
puts header
}
Seems inefficient to check each header entry manually if it's in an excluded directory.
如果每个标题条目位于排除的目录中,则手动检查每个标题条目似乎效率低下。
采纳答案by the Tin Man
Don't use globbing, instead use Find. Find is designed to give you access to the directories and files as they're encountered, and you programmatically decide when to bail out of a directory and go to the next. See the example on the doc page.
不要使用通配符,而是使用Find. Find 旨在让您在遇到目录和文件时访问它们,您可以通过编程方式决定何时退出目录并转到下一个目录。请参阅文档页面上的示例。
If you want to continue using globbing this will give you a starting place. You can put multiple tests in rejector'd together:
如果您想继续使用通配符,这将为您提供一个起点。您可以将多个测试放入reject或放在一起:
Dir['**/*.h'].reject{ |f| f['/path/to/skip'] || f[%r{^/another/path/to/skip}] }.each do |filename|
puts filename
end
You can use either fixed-strings or regex in the tests.
您可以在测试中使用固定字符串或正则表达式。
回答by Jordon Bedwell
I know this is 4 years late but for anybody else that might run across this question you can exclude from Dir the same way you would exclude from Bash wildcards:
我知道这已经晚了 4 年,但是对于可能遇到这个问题的任何其他人,您可以像从 Bash 通配符中排除一样从 Dir 中排除:
Dir["lib/{[!errors/]**/*,*}.rb"]
Which will exclude any folder that starts with "errors" you could even omit the /and turn it into a wildcard of sorts too if you want.
这将排除任何以“错误”开头的文件夹,/如果您愿意,您甚至可以省略它并将其转换为各种通配符。
回答by Theo
There's FileListfrom the Rake gem (which is almost always installed by default, and is included in the standard library in Ruby 1.9):
有FileList来自耙宝石(这几乎总是默认安装的,并且包含在Ruby 1.9的标准库):
files = FileList['**/*.h'].exclude('skip_me')
FileListhas lots of functionality for working with globs efficiently.
FileList有很多功能可以有效地使用 globs。
You can find the documentation here: http://rake.rubyforge.org/classes/Rake/FileList.html
您可以在此处找到文档:http: //rake.rubyforge.org/classes/Rake/FileList.html
回答by grosser
files = Dir.glob(pattern)
files -= Dir.glob("#{exclude}/**/*")
回答by Mark Thomas
One way:
单程:
require 'find'
ignores = ['doc','test','specifications']
Find.find(ENV['HOME']) do |path|
name = File.basename(path)
if FileTest.directory?(path)
if ignores.include?(name)
Find.prune
else
next
end
else
puts path if name =~ /.h$/
end
end
回答by Naftulee
this is similar to a few other answers just written a bit differently
这类似于其他一些答案,只是写得有点不同
Ill create an array that can be passed to a .each iteration or something else.
我将创建一个可以传递给 .each 迭代或其他东西的数组。
release_safelist = Dir.glob('*').reject{|file| (file == "softlinks") || (file == "ci") || (file.include? "SNAPSHOT")}
release_safelist = Dir.glob('*').reject{|file| (文件==“软链接”)|| (文件 == "ci") || (file.include?“快照”)}
In this case im creating an array without files/dir named either ci, softlinks, or containing SNAPSHOT
在这种情况下,我创建了一个没有文件/目录的数组,命名为 ci、softlinks 或包含 SNAPSHOT

