From: Robert Klemme Date: 2009-11-02T17:31:00+09:00 Subject: Re: mtime.max 2009/11/2 Derek Smith : >> Why do you convert the mtime (a Time object) to an integer? Time >> objects are comparable. Try to omit the ".to_i" and compare the >> Time objects that represent the mtimes. > I feel like Im smoking crack and getting pissed off at the same time! No > offence intended! > This is still not working as I should be seeing > /backups/prod_db_bkup_Thr2009-10-291052.gz > as the oldest file. > > >  require 'time' >    stats = Hash.new >    DB_BKUP1.each do |dbfile| >        stats[dbfile] = [File.stat(dbfile).mtime] >    end > >    stats_sorted = Hash.new >    stats_sorted = stats.sort { |x,y| x[1]<=>y[1] } > >    oldest = stats_sorted[0][1].first >    p oldest >    oldestfile = '' >    stats.each_value do |v| >        if  #{v} < #{oldest} >            oldest = v >            oldestfile = stats.index(oldest) >        end >    end >    puts oldestfile >    puts "exit" >    exit > end > > prod_DB_OS_bkup.rb: 108 lines, 2997 characters. > [root@.../usr/local/vrep/OS_scripts]# ruby prod_DB_OS_bkup.rb > Thu Oct 29 10:52:41 -0400 2009 > /backups/prod_db_bkup_Thr2009-10-291054.gz > exit > > [root@.../usr/local/vrep/OS_scripts]# ls -alrt /backups/prod*.gz > -rw-r--r--  1 root  wheel  38608780 Oct 29 10:52 > /backups/prod_db_bkup_Thr2009-10-291052.gz > -rw-r--r--  1 root  wheel  38608793 Oct 29 10:54 > /backups/prod_db_bkup_Thr2009-10-291054.gz > -rw-r--r--  1 root  wheel  38608802 Oct 29 11:00 > /backups/prod_db_bkup_Thr2009-10-29110.gz > -rw-r--r--  1 root  wheel  38609193 Oct 30 10:42 > /backups/prod_db_bkup_Fri2009-10-301042.gz > -rw-r--r--  1 root  wheel  38770872 Nov  1 16:29 > /backups/prod_db_bkup_Sun2009-11-011629.gz IMHO you're making your life too difficult: for the youngest file you can use Enumerable#max_by: 09:25:28 Temp$ ls -lt | head -5 total 339 -rwx------+ 1 RKlemme Domain Users 1544 Nov 2 09:25 etilqs_y5Wb4Ntehb25BGev1S84-journal* -rwx------+ 1 RKlemme Domain Users 184862 Nov 2 09:14 xphoon.bmp* drwx------+ 2 RKlemme Domain Users 0 Nov 2 09:14 notes6030C8/ drwxr-xr-x+ 2 RKlemme Domain Users 0 Nov 2 09:11 hsperfdata_rklemme/ 09:26:07 Temp$ allruby -e 'puts Dir["*"].max_by {|f| File.mtime f}' CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin ======================================== ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] etilqs_y5Wb4Ntehb25BGev1S84-journal ======================================== ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin] etilqs_y5Wb4Ntehb25BGev1S84-journal For the oldest it is Enumerable#min_by: 09:26:41 Temp$ ls -lt | tail -5 -rwx------+ 1 RKlemme Domain Users 663 Feb 6 2009 to_h.rb* -rwxr--r-- 1 RKlemme Domain Users 548 Jan 27 2009 typo-finder.rb* -rwx------+ 1 RKlemme Domain Users 390 Jan 20 2009 req.rb* -rwx------+ 1 RKlemme Domain Users 426 Dec 11 2008 ds.rb* -rwx------+ 1 RKlemme Domain Users 787 Dec 9 2008 drb-demo.rb* 09:26:47 Temp$ allruby -e 'puts Dir["*"].min_by {|f| File.mtime f}' CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin ======================================== ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] drb-demo.rb ======================================== ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin] drb-demo.rb Even if you want the whole array sorted by mtime you can use #sort_by and #last or #first: 09:26:53 Temp$ allruby -e 'ar = Dir["*"].sort_by {|f| File.mtime f}; puts ar.first, ar.last' CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin ======================================== ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] drb-demo.rb etilqs_y5Wb4Ntehb25BGev1S84-journal ======================================== ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin] drb-demo.rb etilqs_y5Wb4Ntehb25BGev1S84-journal Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/