From: John Carter Date: 2003-06-17T08:59:00+09:00 Subject: Re: Samples of daliy use On Mon, 16 Jun 2003, Daniel [UNKNOWN] V�lkerts wrote: > I found Ruby very interesting and I'll be glad if anyone could give some > urls which demonstrates the use of ruby in our daily work ( e.g. common > problems solved with ruby, administrative tasks etc.). Just completed this now. Boss wanted to track from defect number in the defect tracking tool to files changed. ie. Project is forking the code, low tier model going into production, high tier model starting development. Unfortunately at the point of forking the code base and porting it to new platform, there were still a number of defects. My little Ruby scripts parses 'cvs log' output, grabs things that look like defect tracking numbers and chucks it in a file. Another script uses the cgi module to present the results, complete with hot links into web interface of the defect tracking tool and into cvsweb showing exactly which file was changed and what the log entry was. Clip it to my Apache server (standard with Linux), email out the URL and instantly the whole company has access to my results. You've just got to love it. Less than a days work. Ooo I love the develope by iteration style in Ruby. Version 1 looks like this.... File.open( "proj.log") do |log| log.each_line do |line| puts line end end ------------------- Very easy, can see it work. Then the next version.... File.open( "proj.log") do |log| log.each_line do |line| if line =~ /^Working file: (\S+)/ then # Pick up file name with regex puts "File #{$1}" end end end ---------- #Right, I'm finding the file start so lets iterate over files.. def each_file(log) log.each_line do |line| if line =~ /^Working file: (\S+)/ then yield $1 end end end # Now my main loop is very clean... File.open( "proj.log") do |log| each_file(log) do |file| puts file end end ----------------- #Now each file has a bunch of revisions associated with it... def each_revision(log) log.each_line do |line| if line =~ /^revision (\S+)/ then yield $1 end end end def each_file(log) log.each_line do |line| if line =~ /^Working file: (\S+)/ then yield $1 end each_revision(log) do |rev| puts rev end end end # Now my main loop is very clean... File.open( "proj.log") do |log| each_file(log) do |file| puts file end end --------------------- Hmm. I need to package a list of revisions with the file, no problem spin a file class. class CVSFile attr_reader :file_name attr_accessor :head_revision, :revision def initialize( name) @file_name = name @revision = Hash.new end end Hmm I need to package some info with the revision number , no problem spin a class for that. And so I go, alternately adding complexity, and then factoring it out into nested iterations so each iteration is simple. It's just soo blooming easy to do! John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand A Million Monkeys can inflict worse things than just Shakespeare on your system.