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

前端 未结 2 1043
轮回少年
轮回少年 2021-02-19 02:32

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

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

相关标签:
2条回答
  • 2021-02-19 03:07

    Here's a shorter version:

    Dir['{.[^\.]*,*}']
    
    0 讨论(0)
  • 2021-02-19 03:19

    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('.')
    
    0 讨论(0)
提交回复
热议问题