From: Konrad Meyer Date: 2007-10-14T08:06:26+09:00 Subject: Re: top 10 last played mp3's --nextPart1463448.0sYXGqbxM8 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth Andreas S.: > Robin Wagenaar wrote: > > My level of ruby-experience has just slightly passed hello-world, I'm > > quite new to this programming language; I hope I don't bother all of you > > too much by posting my low-level question here. > >=20 > > I would like to make a program that iterates recursively trough my music > > directory, and selects the top-10 of most recently played tracks. > >=20 > > I try to use File.atime(path) to check if the current file is accessed > > recently enough to get into my top-10, but my problem is that I don't > > know how to (correctly) compare a date to the return value of > > File.atime. >=20 > File.atime returns a Time object, you can compare it to another Time=20 > object, for example: >=20 > if File.atime(path) > (Time.now - 60*60) > # file has been accessed in the last hout > end >=20 > But you can also solve the whole problem much easier: >=20 > Dir['/share/music/**/*.mp3', '/share/music/*.mp3'].sort_by{|f|=20 > File.atime}.reverse[0,10] Or, if it's deeper than two levels: require 'find' Find.find("/share/music/").sort_by { |f| f.atime }.reverse[0...10] I'm glad you (the OP) are interested in learning more about ruby. Regards, =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart1463448.0sYXGqbxM8 Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBHEU9vCHB0oCiR2cwRAkCNAKC2vO8oIgkHPzGcE61doMDxmNe0FgCg0J3F 4JW3gQn+01oF69LPuv53sw4= =xQMx -----END PGP SIGNATURE----- --nextPart1463448.0sYXGqbxM8--