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
file = File.new(“testfile”, “r”)# … process the filefile.closetestfile是想要 *** 作的文件名,”r”说明了文件的 *** 作模式为读龋可以使用”w”表亩此缺示写入,”rw”表示读写。最后要记得关闭打开的文件,确迅辩扒扮保所有被缓冲的数据被写入文件,所有相关的资源...欢迎分享,转载请注明来源:内存溢出
评论列表(0条)