有没有一种方法来在glob的红宝石一个目录,但排除某些目录? [英] Is there a way to glob a directory in Ruby but exclude certain directories?

查看:93
本文介绍了有没有一种方法来在glob的红宝石一个目录,但排除某些目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个glob的目录后处理的头文件。然而,我要排除项目中的某些目录。眼下的默认方式为...

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.

推荐答案

不要使用通配符,而是使用的 查找 。发现的目的是给你的目录和正在遇到他们文件的访问,并且您以编程方式决定何时摆脱困境的一个目录中,并进入下一个。看到该文档,页面上的例子。

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.

如果您希望继续使用通配符这会给你一个首发位置。你可以把多个测试在拒绝逻辑或运算,

If you want to continue using globbing this will give you a starting place. You can put multiple tests in reject or'd together:

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.

这篇关于有没有一种方法来在glob的红宝石一个目录,但排除某些目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆