与Dir['*']的Ruby列表目录,包括dotfiles,但不包括。和

时间:2022-06-01 20:20:10

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

如何让目录['*']包含dotfiles,例如.gitignore,但不能。和. . ?

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

即。,有没有更好的办法:

`ls -A`.split "\n"

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

也许与Dir ?下面的解决方案非常接近,但都包括在内。& . .:

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

So, the following works:

所以,以下工作:

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

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

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

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

我想用这个来修正流星自制公式的第9行。

3 个解决方案

#1


14  

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

不能使用Dir[],但可以使用Dir。水珠,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[. ..]

(among other ways)

(在其他方面)

#2


2  

Here's a shorter version:

这里有一个较短的版本:

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

#3


0  

The following works with Dir.glob:

下面是与dirb合作的作品。

Dir.glob("{*,.?*}")

#1


14  

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

不能使用Dir[],但可以使用Dir。水珠,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[. ..]

(among other ways)

(在其他方面)

#2


2  

Here's a shorter version:

这里有一个较短的版本:

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

#3


0  

The following works with Dir.glob:

下面是与dirb合作的作品。

Dir.glob("{*,.?*}")