ruby进行文件行数计算

昨天软件工程课上蛮有收获,提到了Gantt图,项目时间管理等一些比较感兴趣的东西。其中项目复杂度最简单的方法可以通过项目中代码行数确定。逐用ruby 尝试下。

 

代码核心在于获取某文件行数 以及 某文件夹下所有文件的遍历,前者好像找不到什么好的API,我使用的是遍历的方法。后者有很多重方法,就用简单点的Find了,下次尝试用Tree的形式并生成xml

 

PS 本来想弄个后缀过滤,后来发现linux下许多文件都没有后缀的~~~不管了。

##
##  this is design for calculate the totals line of code in your project
##  version 1 @october 8th,2008 
##
 

module Enumerable
  # function to get total lines for file
  def total_lines
    lines = 0
    each_with_index {|content,lines|}
    return lines+1
  end
end


class CheckLines
  require 'find'


  @check_type = %w{txt rb erb yml html css xml}
  def initialize(directory)
    @total_lines = 0
    if  File.directory?(directory)
          @directory = directory 
          @contents = {}
          self.go
    else puts "#{directory} is not a directory! check it out!" and return
    end
  end
  
  def go
    if @directory
      Find.find @directory do |path|
        pathlite = path.gsub(@directory,'')
        if File.file? path
          File.open path do |f|
              tmp_line = f.total_lines  
             @contents.store(pathlite,tmp_line) 
             @total_lines += tmp_line
          end
        end
      end
      puts @total_lines
    end
  end
  
  def details
    @contents.each do |key,value|
      puts "#{key} file has lines of #{value}"
    end
  end
end

 写的时候出了个小问题,在class内定义了HASH @contents={} 然后在details里面调用时报错,提示NilClass,在 initialize里面重新定义一次才能通过,也就是说你写在class 和 function 外面的 东西它是不执行的?

下次改进:读取文件过滤,结果生成XML,然后通过Flex的图表画出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值