Treacle

Java Toxication!
posts(28) comments(15) trackbacks(0)
  • BlogJava
  • 联系
  • RSS 2.0 Feed 聚合
  • 管理

公告

Betake Java
Gape after Ruby!

Contact

Mail me!

Search

Google
Technorati search

Technorati Profile Get Conversations about BlogJava - TreaclePing Technorati

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类(30)

  •  Eclipse(2)
  •  English(5)
  •  Java(6)
  •  Portal(1)
  •  ROR(4)
  •  Self(12)

随笔档案(28)

  • 2008年7月 (2)
  • 2005年9月 (3)
  • 2005年8月 (2)
  • 2005年7月 (2)
  • 2005年6月 (19)

文章分类(4)

  •  Java
  •  Others(3)
  •  ROR(1)

文章档案(2)

  • 2005年6月 (2)

相册

  • MySelf
  • 诗词

收藏夹(2)

  •  Java
  •  Net(1)
  •  Portal(1)
  •  UI

Friends

  •  Salomon's Java Castle

最新评论

  • 1. re: 一款好用的图形转换软件
  • 杂用啊?
  • --李力
  • 2. re: 许久未来了
  • 同感............
  • --久城
  • 3. re: 一款好用的图形转换软件
  • XZXZXZ
  • --213
  • 4. re: AjaxFaces 1.0 released
  • 网页打不开阿,博主能发给我吗,谢谢
  • --gembin
  • 5. re: Ruby概述
  • 惊讶!怎么回复表单中正确地显示了我的所有信息?
  • --靖郭郭

阅读排行榜

  • 1. 一款好用的图形转换软件(1840)
  • 2. 用GOOGLE你瞬间成为黑客(1800)
  • 3. 网络经典命令行(1489)
  • 4. Language Oriented Programming 面向语言编程(1366)
  • 5. Use Ruby send email(1313)

评论排行榜

  • 1. 一款好用的图形转换软件(5)
  • 2. 问dudu一个问题(3)
  • 3. Get total lines of files(2)
  • 4. 许久未来了(1)
  • 5. Ruby概述(1)

View Post

Get total lines of files

This is one small tools to get the total file line number of on folder.

 1POST_FIX = %w{java jsp html xml properties}
 2EXCLUDE_DIR =
 %w{CVS}
 3$displayStr = ""

 4$fileReg = ""
 5$dirReg = "\\.|\\.\\.|"
 6

 7def sumLine(fileName)
 8  file =
 open fileName
 9
  begin
10    while
 file.gets()
11    end

12  ensure
13
    file.close
14  end

15  $.
16end

17
18def iteDir(name, level=0
)
19  lines = 0

20  dir = Dir.open name
21  rex = /\. + "java" +$|\.jsp$|\.html$/

22  begin
23    dir.each do
 |fileName|
24      fileName = dir.path + '/' + fileName

25      if File.ftype(fileName) =~ /directory/
26        if not fileName =~ Regexp.new($dirReg)
27          lines += iteDir(fileName, level+1
)
28        else

29          next
30        end
31      elsif fileName =~ Regexp.new($fileReg)
32        lines +=
 sumLine(fileName)
33      end

34    end
35  ensure
36
    dir.close
37  end

38  str = ""
39  for i in 0..(level-1)
40    str += "\t|"

41  end
42  if level != 0
43    str += "---"
44  end
45  $displayStr = "#{str}#{name}\t#{lines} lines\n#{$displayStr}"
46  lines
47end

48
49POST_FIX.each do
 |postfix|
50  $fileReg += "\.#{postfix}$|"

51end
52$fileReg = $fileReg.chop
53

54EXCLUDE_DIR.each do
 |excludeDir|
55  $dirReg += "^#{excludeDir}$|"

56end
57$dirReg = $dirReg.chop
58

59print "Total #{iteDir("E:/_MyWorkSpace/IDEA_Proj")} lines\n"

60print $displayStr

Keystone:
   1. Ruby I/O access. Dir and File
   2. Regexp & String -> Regexp
   3. Some kinds of variables
   4. Array operation

Technorati Tags: Ruby Ping Technorati

posted on 2005-06-18 12:24 Blake HAN 阅读(1246) 评论(2)  编辑  收藏 所属分类: ROR

View Comments

# re: Get total lines of files  回复  更多评论   
reinventing the wheel:
+ require 'find'
+ IO.readlines(path).size

inefficient:
+ append to arrays, not strings ( a = []; a << ...; a.join )
+ don't create a million regexp objects in a tight loop when one will do ($fileReg = /#{...}/; fileName =~ $fileReg)

-- zenspider
2005-07-23 11:48 | zenspider
# re: Get total lines of files  回复  更多评论   
Thank you Zenspider! Thanks for your good suggestion! I'm a Ruby newbie, I think I can learn much from you!
2005-07-24 00:13 | blake
新用户注册  刷新评论列表  

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问   管理
相关文章:
  • Get total lines of files
  • Use Ruby send email
  • Ruby概述
  • Ruby Http client
 
 
Powered by:
BlogJava
Copyright © Blake HAN