日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
迅速掌握Ruby文件行數(shù)計算技巧

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

代碼核心在于獲取某文件行數(shù) 以及 某文件夾下所有文件的遍歷,前者好像找不到什么好的API,我使用的是遍歷的方法。后者有很多重方法,就用簡單點(diǎn)的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ù)計算的使用技巧介紹。希望對大家有所幫助。


網(wǎng)頁標(biāo)題:迅速掌握Ruby文件行數(shù)計算技巧
標(biāo)題路徑:http://www.dlmjj.cn/article/cdojcii.html