带有Dir ['*']的Ruby列表目录,包括点文件,但不包括.和 [英] Ruby list directory with Dir['*'] including dotfiles but not . and

查看:142
本文介绍了带有Dir ['*']的Ruby列表目录,包括点文件,但不包括.和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取Dir['*']包含点文件(例如.gitignore),但不包括...?

How do I get Dir['*'] to include dotfiles, e.g., .gitignore, but not . and ..?

即,是否有更好的方法:

I.e., is there a better way to do:

`ls -A`.split "\n"

也许用Dir?以下解决方案比较接近,但都包括.& ..:

perhaps with Dir? The following solutions are close but both include . & ..:

Dir.glob('*', File::FNM_DOTMATCH)
Dir['{.*,*}']

因此,以下工作有效:

Dir.glob('*', File::FNM_DOTMATCH) - ['.', '..']

但是,还有更好的方法吗?

But, is there still a better way to do this?

我想知道如何解决流星自制方法的第9行./p>

I'm wondering this to fix line 9 of a Meteor Homebrew Formula.

推荐答案

您不能使用

您可以摆脱.& ..轻松:

You can get rid of the . & .. easily:

Dir.glob("*", File::FNM_DOTMATCH).tap { |a| a.shift(2) }

但是我认为最好还是坚持原来的方式:

But I think it’s probably best to stick with your original way:

Dir.glob("*", File::FNM_DOTMATCH) - %w[. ..]

(除其他方法外)

但是,如果您不需要比*更复杂的glob,请 grep 需要更多过滤的结果):

However, if you don’t require a more sophisticated glob than *, Dir#children may be all you need (can always further filter/grep the results if more filtering is needed):

Dir.children('.')

这篇关于带有Dir ['*']的Ruby列表目录,包括点文件,但不包括.和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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