最近学习ruby写的一个代码行数统计工具。

作者: 刘典
发布时间:2015-07-13 11:31:31

#保存扫描结果  class Result          def initialize                  self.name = ''                  self.count = 0                  self.subResult = Array.new          end          attr_reader :name, :count, :subResult          attr_writer :name, :count, :subResult  end    #行数统计类  class LinesCounter          #dir            目标目录名          #fileFilter     文件过滤器,返回false表示过滤掉。          #lineFilter     行过滤器,返回alse表示过滤掉。          def initialize(dir, fileFilter, lineFilter)                  raise "#{dir} 不是目录" unless File.directory?(dir)                  @dir = dir                  @fileFilter, @lineFilter = fileFilter, lineFilter          end            def count                  r = Result.new                  r.name = @dir                  countDir("", @dir, r)                  return r          end  private          def getPath(prefix, name)                  prefix=='' ? name : prefix + File::SEPARATOR + name          end          def countDir(prefix, dirName, collect)                  path = getPath(prefix, dirName)                  c = 0                  Dir.foreach(path) do                          |x|                          next if x =~/^(\.|\.\.)$/                          r = Result.new                          r.name = x                          subpath = getPath(path, x)                          if File.directory?(subpath)                                  c += countDir(path, x, r)                          else                                  next unless @fileFilter.call(x)                                  c += countFile(path, x, r)                          end                          collect.subResult.push(r)                  end                  collect.count = c          end          def countFile(prefix, fileName, collect)                  path = getPath(prefix, fileName)                  c = 0                  File.foreach(path) do                          |x|                          next unless @lineFilter.call(x)                          c = c + 1                  end                  collect.count = c          end  end    #打印结果  def printResult(r)          def printR(r, deep)                  puts "#{"\t" * deep}#{r.name}(#{r.count})"                  r.subResult.each do                          |x|                          printR(x, deep+1)                  end          end          printR(r, 0)  end    lc = LinesCounter.new(ARGV[0],\          proc {|x| x =~ /^.*\.rb$/} ,            #只统计*.rb文件\          proc {|x| x !~ /^\s*(#|\s*$)/ })        #忽略注释和空行  
#这里可以根据自己情况修改

printResult(lc.count)

使用方法和运行结果:    D:\ruby>ruby lines.rb D:\ruby  D:\ruby(241)          a.rb(5)          exp.rb(130)          hello(1)                  hello.rb(1)          lines.rb(69)          other(20)                  pipe.rb(16)                  scan.rb(4)          pipe.rb(16)    

标签: Ruby
来源:http://www.cnblogs.com/theseus/articles/239662.html

推荐: