[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

make this code more Rubyish

Jason Sweat

7/7/2006 11:40:00 PM

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/...) 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...

6 Answers

dblack

7/8/2006 12:25:00 AM

0

Jason Sweat

7/8/2006 1:53:00 AM

0

On 7/7/06, dblack@wobblini.net <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...

William James

7/8/2006 5:18:00 AM

0

Jason Sweat wrote:
> 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/...) 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...

dev = <<EOM
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)
EOM

prod = <<EOM
2609 tests php=2598,sh=8
1460 models php=1462
321 views php=325
1257 actions php=257
327 top_dir php=227
84 sql sh=84
0 CVS (none)
0 img (none)
0 templates (none)
0 tutorials (none)
EOM

puts "Dir\tProd\tDev\tDiff",
[ prod, dev ].map{|x| x.split(/\s+\S+\n/).map{|s| s.split} }.
transpose.map{|x| x.transpose}.map{|n,s|
[ s[0], n[0], n[1], n[1].to_i - n[0].to_i]}.select{|x|
x[1,2]!=["0","0"]}.sort_by{|x| x[-1]}.map{|x| x.join("\t")}.reverse


Dir Prod Dev Diff
views 321 325 4
models 1460 1462 2
sql 84 84 0
tests 2609 2606 -3
top_dir 327 227 -100
actions 1257 257 -1000

dblack

7/8/2006 5:23:00 PM

0

dblack

7/8/2006 5:26:00 PM

0

Ashley Moran

7/8/2006 7:15:00 PM

0


On Jul 08, 2006, at 6:26 pm, dblack@wobblini.net wrote:

>>> puts "Dir\tProd\tDev\tDiff",
>>> [ prod, dev ].map{|x| x.split(/\s+\S+\n/).map{|s| s.split} }.
>>> transpose.map{|x| x.transpose}.map{|n,s|
>>> [ s[0], n[0], n[1], n[1].to_i - n[0].to_i]}.select{|x|
>>> x[1,2]!=["0","0"]}.sort_by{|x| x[-1]}.map{|x| x.join("\t")}.reverse
>>
>> He's done it in Perl already -- he said more Rubyish :-)
>
> Whoops, my witty repartee was factually inaccurate. s/in Perl/in
> Ruby but in a more typically Perlish style/ :-)
>
>
> David


I actually thought you were being metaphorical with that.. that code
is the most Perlish Ruby I've ever seen.

It's been said a million times that you can write FORTRAN in any
language; judging by the snippet above I think it's fair to say you
can write any language in Ruby!

Ashley