From: Jason Sweat Date: 2006-07-08T10:52:44+09:00 Subject: Re: make this code more Rubyish On 7/7/06, dblack@wobblini.net wrote: > I think there's a logic flaw there. If you have two directories with > the same diff count, the second one will clobber the hash entry from > the first one. Yes, I actually noticed that as I was running it on some other projects. Amazing how your first sample can taint your perspective. Does this iteration look a bit less like line noise? require 'scanf' def sloc_scandir(dir) /^SLOC.*?$(.*(?=^Totals grouped by))/im.match( %x{sloccount --addlangall #{dir} 2>/dev/null} )[1].to_s end def get_counts(lines) res = {} lines.scanf("%i%s%s") do |count, thing| next unless count > 0 res[thing] = count end res end class SlocDir attr_accessor :name, :dev, :prd def initialize(name, dev) @name = name @dev = dev end def diff @dev - @prd end def render "#{@name}\t#{@prd}\t#{@dev}\t#{diff}" end end dirs = {} get_counts(sloc_scandir('.')).each_pair { |k,v| dirs[k] = SlocDir.new(k,v) } get_counts(sloc_scandir(`cat .prod_dir`)).each_pair { |k,v| dirs[k].prd = v } puts "Dir\tProd\tDev\tDiff" dirs.values.sort{ |x,y| y.diff <=> x.diff }.each { |d| puts d.render } Regards, Jason http://blog.casey-sweat.us/