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

本文介绍了一个简单的代码行统计工具的实现方法。该工具能够遍历指定路径下的所有文件,识别并统计 Java、JavaScript 和 JSP 文件中的代码行数,同时过滤掉注释部分。通过对文件的逐行读取和特定模式匹配,确保了统计结果的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值