How do I get a listing of only files using Dir.glob?

前端 未结 8 542
走了就别回头了
走了就别回头了 2021-01-03 21:10

How can I return a list of only the files, not directories, in a specified directory?

I have my_list = Dir.glob(script_path.join(\"*\"))

This re

8条回答
  •  抹茶落季
    2021-01-03 21:29

    If you want to do it in one go instead of first creating an array and then iterating over it with select, you can do something like:

    my_list = []
    Dir.foreach(dir) { |f| my_list << f if File.file?(f) }
    

提交回复
热议问题