How can you find the most recently modified folder in a directory using Ruby?

前端 未结 3 332
逝去的感伤
逝去的感伤 2021-01-18 20:38

How can you find the most recently modified folder (NOT A FILE) in a directory using Ruby?

3条回答
  •  星月不相逢
    2021-01-18 21:35

    Dir.glob("a_directory/*/").max_by {|f| File.mtime(f)}
    

    Dir.glob("a_directory/*/") returns all the directory names in a_directory (as strings) and max_by returns the name of the directory for which File.mtime returns the greatest (i.e. most recent) date.

    Edit: updated answer to match the updated question

提交回复
热议问题