问题描述
如何获取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.
推荐答案
您不能使用 Dir[]
,但您可以使用 Dir.glob
,它被Dir[]
调用:
Dir.glob("*", File::FNM_DOTMATCH)
您可以摆脱.
& ..
轻松:
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,请 Dir#children
可能就是您所需要的(总是可以进一步 filter
/ 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列表目录,包括点文件,但不包括.和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!