From: Brian Adkins Date: 2007-10-14T10:55:03+09:00 Subject: Re: top 10 last played mp3's Hmm.. it just occurred to me that many of the solutions presented here have the flaw of potentially calling File.atime() multiple times for the same file which would require unnecessary calls to the operating system to get the access time of the file. Better to compute atime only once and then sort on the result. In the case below, I then extract the file name back out of the resulting array, but in actuality, there may be some value in providing that to the caller in case they could make use of the access time values. require 'pp' require 'find' def n_recent_files n = 1, path = '.', exts = nil paths = [] Find.find(path) do |p| if File.file?(p) && ( exts.nil? || ( (ext = File.extname(p)) && !ext.empty? && exts.include?(ext) ) ) paths << [ p, File.atime(p).to_i ] end end paths.sort! {|a,b| b[1] <=> a[1] }[0,n].map {|x| x[0]} end pp n_recent_files(10, '/home/brian/temp', '.rb') pp n_recent_files(10, '/home/brian/temp', ['.rb', '.txt'])