看代碼code
# encoding: utf-8 class CodeLineStat attr_reader :code_lines def initialize @code_lines = 0 end def stat(path) Dir.foreach(path) do |file| if file != "." && file != ".." then filePath = path + "/" + file if File.directory? filePath then stat(filePath); elsif file =~ /(\.cs|\.aspx)$/ then @code_lines += file_code_line(filePath); end end end end private def file_code_line(filePath) lines = 0 File.open(filePath,"r:utf-8") do |file| in_comment = false; file.each_line do |line| line.strip! if line.index("/*") then in_comment = true elsif line.index("*/") then in_comment = false elsif !line.empty? && !in_comment && !line.index("//") then lines += 1 end end end puts "#{filePath} : #{lines}" lines end end cl = CodeLineStat.new cl.stat(ARGV[0]) title = "總共有代碼行:".encode('big5') puts cl.code_lines