昨天软件工程课上蛮有收获,提到了Gantt图,项目时间管理等一些比较感兴趣的东西。其中项目复杂度最简单的方法可以通过项目中代码行数确定。逐用ruby 尝试下。
代码核心在于获取某文件行数 以及 某文件夹下所有文件的遍历,前者好像找不到什么好的API,我使用的是遍历的方法。后者有很多重方法,就用简单点的Find了,下次尝试用Tree的形式并生成xml
PS 本来想弄个后缀过滤,后来发现linux下许多文件都没有后缀的~~~不管了。
##
## this is design for calculate the totals line of code in your project
## version 1 @october 8th,2008
##
module Enumerable
# function to get total lines for file
def total_lines
lines = 0
each_with_index {|content,lines|}
return lines+1
end
end
class CheckLines
require 'find'
@check_type = %w{txt rb erb yml html css xml}
def initialize(directory)
@total_lines = 0
if File.directory?(directory)
@directory = directory
@contents = {}
self.go
else puts "#{directory} is not a directory! check it out!" and return
end
end
def go
if @directory
Find.find @directory do |path|
pathlite = path.gsub(@directory,'')
if File.file? path
File.open path do |f|
tmp_line = f.total_lines
@contents.store(pathlite,tmp_line)
@total_lines += tmp_line
end
end
end
puts @total_lines
end
end
def details
@contents.each do |key,value|
puts "#{key} file has lines of #{value}"
end
end
end
写的时候出了个小问题,在class内定义了HASH @contents={} 然后在details里面调用时报错,提示NilClass,在 initialize里面重新定义一次才能通过,也就是说你写在class 和 function 外面的 东西它是不执行的?
下次改进:读取文件过滤,结果生成XML,然后通过Flex的图表画出来。