From: "Anders Engström" Date: 2002-12-16T18:01:03+09:00 Subject: Re: Splitomania - advice welcome On Sat, 14 Dec 2002 05:48:19 +0900, Matt Armstrong thus spoketh: > Anders Engstr�m writes: > >> Hi. >> >> I'm writing a commitinfo script for CVS using ruby 1.6.7 on Debian >> Woody. I've created a class that handles all the ugly stuff at a commit, >> and the actual commitinfo-scripts uses this class to ease the CVS >> interaction. >> >> Now - I want to extract the editors of each file that gets commited. >> This is done by parsing the $CVSHOME/CVS/fileattr file. I want to get >> all editors as a Hash where the keys are the names of the editors, and >> the values are the extra info for the editor. The file format of >> $CVSHOME/CVS/fileattr is something like: >> >> Fname_of_fileattribute_name=attribute_value[;attribute_name=attribute_value...] >> >> the attribute containing the editors looks like: >> >> _editors=name_of_editor>time+hostname+pathname[,name_of_editor>time+hostname+pathname...] > > I suspect the ugly code reflects the ugly file format you are > parsing. Here is my untested attempt. > > editors = Hash.new > > File.open("fileattr") { |line| > fname, attrs = line.split("\t", 2) > attrs.split(";").each { |attr| > next unless attr =~ /^_editors=(.+)/ > $1.split(",").each { |editor| > name, stuff = editor.split(">") > editors[name] = stuff > } > } > } Thanks for the input! This is my current code after applying your ideas: def editors if @editors == nil @editors = Hash.new fileattr = "#{@cvswd}/CVS/fileattr" if File.exist?(fileattr) File.open(fileattr){ |file| line = nil file.each_line { |l| line = l if l =~ /^F#{name}/ } if line != nil line.split("\t").last.split(";").each { |attpair| next unless attpair =~ /^_editors=(.+)/ $1.split(",").each { |editor| name, stuff = editor.split(">") @editors[name] = stuff } } end } end end return @editors end the code is implemented in an accessor method (I don't know if the above is the prefered way to implement 'lazy loading' in Ruby - comments are welcome :). Any comments on the above code? Can I optimize it further? //Anders -- /** * Anders Engstr�m, aengstrom@gnejs.net * ------------------------------------- * Your mind is like an umbrella. * It doesn't work unless you open it. * /Frank Zappa */