Ruby: Finding most recently modified file

偶尔善良 提交于 2020-01-30 17:47:40

问题


What's an idiomatic way to find the most recently modified file within a directory?


回答1:


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



回答2:


Dir["*"].sort { |a,b| File.mtime(a) <=> File.mtime(b) }.last

This is not recursive.




回答3:


I'm not sure if there really is an idiom for this. I would do

Dir["*"].sort_by { |file_name| File.stat(file_name).mtime }

Edit

Seeing how three people gave more or less the same answer at the same time. This must be it.



来源:https://stackoverflow.com/questions/4823507/ruby-finding-most-recently-modified-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!