自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

迅速掌握Ruby文件行數(shù)計算技巧

開發(fā) 開發(fā)工具
Ruby文件行數(shù)計算對于一個初學者來說還是比較難以掌握的,在下面這篇文章中,我們將會學到一些關于Ruby文件行數(shù)計算的相關技巧。

對于Ruby的理解,我們需要不斷的從實際代碼編寫中去總結經(jīng)驗,提升我們對這項語言的理解程度。在這里我們就為大家介紹一種技巧,關于Ruby文件行數(shù)計算的相關技巧。#t#

 

代碼核心在于獲取某文件行數(shù) 以及 某文件夾下所有文件的遍歷,前者好像找不到什么好的API,我使用的是遍歷的方法。后者有很多重方法,就用簡單點的Find了,下次嘗試用Tree的形式并生成xml

 

PS 本來想弄個后綴過濾,后來發(fā)現(xiàn)linux下許多文件都沒有后綴的~~~不管了。

Ruby文件行數(shù)計算代碼示例:

  1. module Enumerable   
  2. # function to get total lines for file   
  3. def total_lines   
  4. lines = 0   
  5. each_with_index {|content,lines|}   
  6. return lines+1   
  7. end   
  8. end   
  9. class CheckLines   
  10. require 'find'   
  11. @check_type = %w{txt rb erb yml html css xml}   
  12. def initialize(directory)   
  13. @total_lines = 0   
  14. if File.directory?(directory)   
  15. @directorydirectory = directory   
  16. @contents = {}   
  17. self.go   
  18. else puts "#{directory} is not a directory! check it out!" and return   
  19. end   
  20. end   
  21. def go   
  22. if @directory   
  23. Find.find @directory do |path|   
  24. pathpathlite = path.gsub(@directory,'')   
  25. if File.file? path   
  26. File.open path do |f|   
  27. tmp_line = f.total_lines   
  28. @contents.store(pathlite,tmp_line)   
  29. @total_lines += tmp_line   
  30. end   
  31. end   
  32. end   
  33. puts @total_lines   
  34. end   
  35. end   
  36. def details   
  37. @contents.each do |key,value|   
  38. puts "#{key} file has lines of #{value}"   
  39. end   
  40. end   
  41. end  

以上就是Ruby文件行數(shù)計算的使用技巧介紹。希望對大家有所幫助。

責任編輯:曹凱 來源: javaeye.com
相關推薦

2009-12-18 14:19:45

Ruby on Rai

2009-12-16 10:57:16

Ruby文件鎖

2009-12-14 15:54:34

Ruby轉義字符

2009-12-14 17:23:31

2009-12-14 18:23:38

Ruby DSL測試

2009-12-04 14:23:33

PHP JSON加密函

2009-12-17 14:36:57

Ruby on Rai

2009-12-16 17:31:30

Ruby on Rai

2009-12-16 11:04:51

Ruby操作文件權限

2009-12-16 10:10:16

Ruby打開關閉文件

2010-01-07 11:07:20

VB.NET讀取INI

2009-12-10 14:18:22

PHP導出Word文檔

2009-12-14 09:33:04

Ruby安裝

2010-01-12 10:19:02

VB.NET操作GDI

2009-12-15 10:23:23

Ruby應用技巧

2009-10-29 13:38:05

VB.NET Shar

2010-01-11 14:28:14

VB.NET操作Exc

2009-12-16 10:49:42

Ruby操作二進制文件

2009-12-09 17:56:27

PHP加密解密

2017-10-11 08:51:50

Ruby編程微服務架構邊緣計算
點贊
收藏

51CTO技術棧公眾號