写个小脚本统计一下最近写的代码行数

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 =~ /(\.java|\.js|\.jsp)$/ then
					@code_lines += file_code_line(filePath);	
				end 
			end
		end
	end

private
	def file_code_line(filePath)
		lines = 0
		File.open(filePath,"r") 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])
puts cl.code_lines

你可能感兴趣的:(jsp,Ruby)