~_~今天周六,明天写文档,所以今天又看了一些Ruby的东西。贴代码。
另外贴一个Vim下Ruby自动补全的配置。
http://hooopo.iteye.com/blog/426782
?
??? 1 IO类
io = open("foo.txt"){|eo| while line = eo.gets puts line end } io = open("foo.txt") io.close #判断io对象是否关闭 p io.closed? require "open-uri" #通过HTTP读取数据 open("http://www.ruby-lang.org"){|io| puts io.read } io = open("foo.txt") while line = io.gets line = chomp! end p io.eof? io.close io = open("foo.txt") while line = io.gets printf("%3d %s",io.lineno, line) end io.close puts "a","b","c" #pos方法 io = open("foo.txt") p io.read(5) p io.pos io.pos io.pos = 0 p io.gets #将文件指针移到文件最前端 io.rewind p io.pos p io.gets puts "-------------------------练习----------------------" def fun(filename) io = open(filename) l = 0 w = 0 c = 0 while line = io.gets l += 1 c += line.size line.sub!(/^\s+/,"") ary = line.split(/\s+/).size w += ary.size end printf(" 行数 单词数 字符数 文件名\n") printf("%8d %8d %8d %s\n",l,w,c,filename) end fun("foo.txt")
#建立正则表达式 r1 = /Ruby/ p r1 r2 = Regexp.new("Ruby") p r2 r3 = %r(Ruby) p r3 #^ $分别表示行头与行尾 p /^ABC$/ =~ "012\nABC" #!注意匹配的是行头行尾,非字符串头与尾 print str = "ABC\nABC" #ABC #ABC #\A \Z分别表示字符串头与字符串尾 p /\AABC\Z/ =~ str #=>nil #[]表示与其中任何一个匹配 p /[012ABC]/ =~ "A" #=>0 p /[012ABC]/ =~ "ACD" #=>0 #[-]表示范围 p /[A-Za-z]/ =~ "1dshhuSSFF" #=>1 p /[A-Za-z_-]/ =~ "12--" #=>2 p /[^A-Za-z_-]/ =~ "--" #=>nil #.代表一个字,不管是什么 p /A.C/ =~ "A1C" #=>0 p /A..C/ =~ "A11C" #=>0 p /A..C/ =~ "A1C" #=>nil #\s表示空白(空格,tab,换行,换页) p /A\sB/ =~ "A B" #=>0 p /A\sB/ =~ "A\tB" #=>0 p /A\sB/ =~ "A\nB" #=>0 p /A\sB/ =~ "AB" #=>nil #\d与0~9匹配 #\w与英文和数字匹配 #*表示前面紧挨着的一个字符出现与不出现都匹配 #+表示前面紧挨着的一个字符出现才匹配 #?表示前面紧挨着的一个字符出现1次或者不出现才匹配 p /12A*/ =~ "" #=>nil p /12A*/ =~ "12" #=>0 p /^Subject:\s*.*$/ =~ "Subject: foo" #?前面的字符为.,表示任意字符,也就是说任意字符出现或出现一次 #.?两个加一起是一个字符或没有字符 p /A.?C/ =~ "AC" #=>0 p /A.C/ =~ "AC" #=>nil #*? +?最短匹配 p "ABCDABCDABCD".slice(/A.*B/) #=>"ABCDABCDAB" p "ABCDABCDABCD".slice(/A.*?B/) #=>"AB" #()配合* + ?表示多个字的反复 p /(ABC)+/ =~ "daABCABC" #表示括弧里匹配结果 p $1 #|表示多选 p /(AB|CD)/ =~ "CD" p $1 #$’$&$‘中间的表示匹配部分 p $’ #=>nil p $& #=>"CD" p $‘ #=>nil #sub,gsub会取代字符串,不同之处在于sub只取代第一处 #gsub全部取代 str = "THis is a man" p str.downcase!.capitalize!.gsub!(/\s./){|matched| #=>"This Is A Man" matched.upcase } p str #=>"This Is A Man" #scan只查找所有匹配的,不取代 "abraca".scan(/.a/){|matched| p matched #=>"ra" } #=>"ca" "abraca".scan(/(.)(a)/){|a,b| p a + b #=>"ra" } #=>"ca" "abraca".scan(/(.)(a)/){|matched| #=>["r","a"] p matched #=>["c","a"] } puts "----------------------------练习-----------------------" p /(.*)@(.*)/ =~ "dsd@gmail.com" p $1 p $2 str = "in-rEp-to" p str.downcase!.capitalize!.gsub!(/-./){|matched| matched.upcase }
??? 3 Dir类和File类
def traverse(path) if FileTest.directory?(path) dir = Dir.open(path) p dir while name = dir.read next if name == "." next if name == ".." traverse(path + "/" + name) end dir.close else process_file(path) end end def process_file(path) puts path end traverse("/home/anity/xxx") #/home/anity/xxx/aa p Dir.pwd p File.expand_path("bin") p Dir.pwd p File.expand_path("../bin") require 'etc' include Etc st = File.stat("/home/anity/ruby") pw = getpwuid(st.uid) p pw.name gr = getgrgid(st.gid) p gr.name filename = "/home/anity/ruby/foo.txt" open(filename,"w").close st = File.stat(filename) p st.ctime p st.mtime p st.atime File.utime(Time.now - 100 , Time.now - 100 ,filename) st = File.stat(filename) p st.ctime p st.mtime p st.atime p File.basename("/home/anity/ruby/foo.txt") p File.basename("/home/anity/ruby/foo.txt",".txt") p File.dirname("/home/anity/ruby/foo.txt") p File.dirname("foo.txt") p File.extname("/home/anity/ruby/foo.txt") p File.split("/home/anity/ruby/foo.txt") dir , base = File.split("/home/anity/ruby/foo.txt") p dir p base p File.join(dir , base)
??? 4 Time类,DateTime类,Date类
t = Time.new p t t = Time.now p t p t.year p t.month p t.day p t.hour p t.min p t.sec p t.to_i p t.wday p t.mday p t.yday p t.zone t = Time.mktime(2009,12,11,3,15,59) p t p t.strftime("%Y/%m/%d %H:%M:%S") p t.strftime("%a %b %d %H:%M:%S %Z:%Y") t = Time.now p t t.utc p t require "date" t1 = DateTime.now sleep(1) t2 = DateTime.now p t2 - t1 t = DateTime.now puts t puts t + 10 require "date" d = Date.today puts d.year puts d.month puts d.day puts d puts Date.new(2009,2,-1) puts d + 1 puts d - 2 puts d >> 3 puts d << 10
?基础居多,需要多加练习。:)