Ruby list directory with Dir['*'] including dotfiles but not . and

…衆ロ難τιáo~ 提交于 2019-12-03 23:35:57

You can't with Dir[], but you can with Dir.glob, which Dir[] calls:

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[. ..]

(among other ways)

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('.')

Here's a shorter version:

Dir['{.[^\.]*,*}']

The following works with Dir.glob:

Dir.glob("{*,.?*}")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!