On Oct 13, 7:50 pm, Brian Adkins <lojicdot... / gmail.com> wrote:
> 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.

Really? Which ones? You do realize that #sort_by is explicitly
designed to call the comparison method exactly once for each object,
right?

>From the ri docs themselves:

"A more efficient technique is to cache the sort keys (modification
times in this case) before the sort. Perl users often call this
approach a Schwartzian Transform, after Randal Schwartz. We construct
a temporary array, where each element is an array containing our sort
key along with the filename. We sort this array, and then extract the
filename from the result."

My understanding is that with a directory containing 5,000 MP3s, this
solution:

  Dir['/share/music/**/*.mp3', '/share/music/*.mp3'].sort_by{ |f|
    File.atime(f)
  }.reverse[0,10]

will call File.atime exactly 5,000 times and create exactly 5,000 Time
instances.