Ruby文件行数计算代码示例:
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)
@directorydirectory = 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|
pathpathlite = 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
我写了个程序 你看一下 不明白的话联系我 有注释#获取当前的时间
today = Time.new
puts "当前日期:" + today.strftime("%Y-%m-%d %H:%M:%S")#采用格式化输出
#获取指定日期的时间 如 2010-08-30 20:50:01
day = Time.local(2010, 8 , 30 , 20, 50, 1)
puts "指定日期:" + day.strftime("%Y-%m-%d %H:%M:%S")
#获取指定日期的秒数(就是1970年到指定日期的秒数)
sec1 = day.to_i
#同理,获取今天的秒数
sec2 = today.to_i
#秒数求差,同时折合为天数
num = (sec1 - sec2)/(24*60*60)
puts (day.strftime("%Y-%m-%d %H:%M:%S") + "与" + today.strftime("%Y-%m-%d %H:%M:%S") + "相差" + num.to_s + "天")