From: William James Date: 2007-08-11T05:54:59+09:00 Subject: Re: Sorting a logfile, how would you write it? On Aug 10, 1:29 pm, Frank Meyer wrote: > I've written a little ruby program which can sort logfiles with the > following format: > > 4.text text text > 1.text text text > 2.text text text > 10.text text text > 2.text2 text2 text2 > > The file is given as a command line parameter and after sorting the > entries it writes them back into this file. > > The program is in the attachement. > > What I want to know is how would you write such a tool in ruby? I'm > asking this because I'm still learning ruby and I want to learn how to > do it in ruby (ans its design principles). > > Thank you! > > Turing > > Attachments:http://www.ruby-forum.com/attachment/86/test.rb File.open( ARGV.first, "r+" ){|file| array = file.readlines file.rewind file.truncate(0) file.puts array.sort_by{|s| s[/^\d+/].to_i } }