I have the following code I pounded out today. It works, but it feels
more like Perl than Ruby to me. Can anyone offer some suggestions for
how to improve the readability of it?
dev = /^SLOC.(?=^Totals grouped by)/im.match(sloccount --addlangall . 2>/dev/null
)
prd = /^SLOC.(?=^Totals grouped by)/im.match(sloccount --addlangall $(cat .prod_dir) 2>/dev/null
)
dh = {}; ph = {}
dev.to_s.scan(/^(\d+)\s+(\w+)/).each { |p| dh[p[1]] = p[0] if p[0].to_i
0 }
prd.to_s.scan(/^(\d+)\s+(\w+)/).each { |p| ph[p[1]] = p[0] if p[0].to_i
0 }
df = {}; dfa = []
ph.each_pair do |k,v|
dfa << diff = dh[k].to_i-v.to_i
df[diff] = “#{k}\t#{v}\t#{dh[k]}\t#{diff}”
end
dfa.sort! {|x,y| y <=> x }
puts “Dir\tProd\tDev\tDiff”
dfa.each { |k| puts df[k] }
The code shells out to the program sloccount
(http://www.dwheeler.com/sloccount/) which produces some output which
looks like this (there is more, but the chunk I am interested in is
contained in this sample):
Categorizing files.
Computing results.
SLOC Directory SLOC-by-Language (Sorted)
2606 tests php=2598,sh=8
1462 models php=1462
325 views php=325
257 actions php=257
227 top_dir php=227
84 sql sh=84
0 CVS (none)
0 img (none)
0 templates (none)
0 tutorials (none)
Totals grouped by language (dominant language first):
php: 4869 (98.15%)
sh: 92 (1.85%)
Total Physical Source Lines of Code (SLOC) = 4,961
The output my Ruby script produces looks like this:
$ sloc_comp.rb
Dir Prod Dev Diff
sql 3474 4858 1384
top_dir 81 81 0
actions 244 241 -3
views 430 424 -6
tests 437 416 -21
models 735 618 -117
Regards,
Jason
http://blog.casey-sweat.us/