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
写个小脚本统计一下最近写的代码行数
最新推荐文章于 2021-01-21 04:42:42 发布
本文介绍了一个简单的代码行统计工具的实现方法。该工具能够遍历指定路径下的所有文件,识别并统计 Java、JavaScript 和 JSP 文件中的代码行数,同时过滤掉注释部分。通过对文件的逐行读取和特定模式匹配,确保了统计结果的准确性。
554

被折叠的 条评论
为什么被折叠?



