开发者

Ruby: Finding most recently modified file

开发者 https://www.devze.com 2023-02-07 08:22 出处:网络
What\'s an idiomatic way to find the most recently modified file within a director开发者_Go百科y?Dir.glob(\"*\").max_by {|f| File.mtime(f)}

What's an idiomatic way to find the most recently modified file within a director开发者_Go百科y?


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


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

This is not recursive.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消