msnews.microsoft.com
12/19/2008 12:30:00 AM
Just a pet peeve.... but... Sorts are expensive and should be avoided
when possible. Finding min and max values are prime examples.
So...
newest = Dir.entries(dir).max {|a,b| (File.mtime(File.join(dir,a)) <=>
File.mtime(File.join(dir,b)))}
On Dec 18, 2008, at 5:42 PM, Robert Klemme wrote:
> On 18.12.2008 20:52, Mark Thomas wrote:
>> On Dec 18, 12:36 pm, Mmcolli00 Mom <mmc_coll...@yahoo.com> wrote:
>>> If you have a directory full of files, how can you get the newest
>>> file
>>> and oldest file from that directory?
>> One way...
>> ages = Dir["C:/New"].map{|f| [f,File.mtime(f)] }.sort_by{|i|i[1]}
>
> IMHO that should read
>
> ages = Dir.entries("C:/New").map{|f| [f,File.mtime(File.join("C:/
> New",f))] }.sort_by{|i|i[1]}
>
> But:
>
>> Or, if you want it in one fell swoop:
>> newest, oldest = Dir["C:/New"].map{|f| [f,File.mtime(f)] }.sort_by{|
>> i|i[1]}.map{|i| i[0]}.values_at(0,-1)
>
> We can do better, i.e. #map is superfluous
>
> sorted = Dir.entries(dir).sort_by {|f| File.mtime(File.join(dir,f))}
> puts sorted.first, sorted.last
>
> Kind regards
>
> robert
>
>
> --
> remember.guy do |as, often| as.you_can - without end
>