偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

 

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

 

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

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

  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ù)計(jì)算的使用技巧介紹。希望對大家有所幫助。

責(zé)任編輯:曹凱 來源: javaeye.com
相關(guān)推薦

2009-12-18 14:19:45

Ruby on Rai

2009-12-16 10:57:16

Ruby文件鎖

2009-12-14 15:54:34

Ruby轉(zhuǎn)義字符

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-10 14:18:22

PHP導(dǎo)出Word文檔

2010-01-07 11:07:20

VB.NET讀取INI

2009-12-16 10:10:16

Ruby打開關(guān)閉文件

2009-12-16 11:04:51

Ruby操作文件權(quán)限

2009-10-29 13:38:05

VB.NET Shar

2010-01-11 14:28:14

VB.NET操作Exc

2010-01-12 10:19:02

VB.NET操作GDI

2009-12-15 10:23:23

Ruby應(yīng)用技巧

2009-12-14 09:33:04

Ruby安裝

2009-12-09 17:56:27

PHP加密解密

2009-12-16 10:49:42

Ruby操作二進(jìn)制文件

2017-10-11 08:51:50

Ruby編程微服務(wù)架構(gòu)邊緣計(jì)算
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號