Sort order in `Dir.entries`

前端 未结 4 2046
逝去的感伤
逝去的感伤 2020-12-03 07:33

Is there a fixed/default sort order in which Dir.entries returns results? I know by experience that the first two entries are \".\" and \"..\

4条回答
  •  伪装坚强ぢ
    2020-12-03 08:12

    Expanding on @maerics' answer ,the below ignores . && .. , regex based filter , and select the latest file if desired.

        Dir.chdir(in_dir)
        target_file   =  Dir.entries(in_dir).select(|x| 
                         x != '.' && 
                         x != '..' && 
                         x =~ /\somefile.txt\z/).sort_by{|f|File.mtime(f)}.last(1)
       puts "here i am #{target_file}"
    

提交回复
热议问题