读书人

Ruby入门学习之4

发布时间: 2012-09-09 09:27:54 作者: rapoo

Ruby入门学习之四

~_~今天周六,明天写文档,所以今天又看了一些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!endp io.eof?io.closeio = open("foo.txt")while line = io.gets  printf("%3d %s",io.lineno, line)endio.closeputs "a","b","c"#pos方法io = open("foo.txt")p io.read(5)p io.posio.posio.pos = 0p io.gets#将文件指针移到文件最前端io.rewindp io.posp io.getsputs "-------------------------练习----------------------"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)endfun("foo.txt")

??? 2 Regexp(正则表达式类)

#建立正则表达式r1 = /Ruby/p r1r2 = Regexp.new("Ruby")p r2r3 = %r(Ruby)p r3#^ $分别表示行头与行尾p /^ABC$/ =~ "012\nABC"#!注意匹配的是行头行尾,非字符串头与尾print str = "ABC\nABC"#ABC#ABC#\A \Z分别表示字符串头与字符串尾p /\AABC\Z/ =~ str #=>nil#[]表示与其中任何一个匹配p /[012ABC]/ =~ "A"#=>0p /[012ABC]/ =~ "ACD"#=>0#[-]表示范围p /[A-Za-z]/ =~ "1dshhuSSFF"#=>1p /[A-Za-z_-]/ =~ "12--"#=>2p /[^A-Za-z_-]/ =~ "--"#=>nil#.代表一个字,不管是什么p /A.C/ =~ "A1C"#=>0p /A..C/ =~ "A11C"#=>0p /A..C/ =~ "A1C"#=>nil#\s表示空白(空格,tab,换行,换页)p /A\sB/ =~ "A B"#=>0p /A\sB/ =~ "A\tB"#=>0p /A\sB/ =~ "A\nB"#=>0p /A\sB/ =~ "AB"#=>nil#\d与0~9匹配#\w与英文和数字匹配#*表示前面紧挨着的一个字符出现与不出现都匹配#+表示前面紧挨着的一个字符出现才匹配#?表示前面紧挨着的一个字符出现1次或者不出现才匹配p /12A*/ =~ ""#=>nilp /12A*/ =~ "12"#=>0p /^Subject:\s*.*$/ =~ "Subject: foo"#?前面的字符为.,表示任意字符,也就是说任意字符出现或出现一次#.?两个加一起是一个字符或没有字符p /A.?C/ =~ "AC"#=>0p /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 $’#=>nilp $&#=>"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 $1p $2str = "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)  endenddef process_file(path)  puts pathendtraverse("/home/anity/xxx")           #/home/anity/xxx/aap Dir.pwdp File.expand_path("bin")p Dir.pwdp File.expand_path("../bin")require 'etc'include Etcst = File.stat("/home/anity/ruby")pw = getpwuid(st.uid)p pw.namegr = getgrgid(st.gid)p gr.namefilename = "/home/anity/ruby/foo.txt"open(filename,"w").closest = File.stat(filename)p st.ctimep st.mtimep st.atimeFile.utime(Time.now - 100 , Time.now - 100 ,filename)st = File.stat(filename)p st.ctimep st.mtimep st.atimep 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 dirp basep File.join(dir , base)

??? 4 Time类,DateTime类,Date类

t = Time.newp tt = Time.nowp tp t.yearp t.monthp t.dayp t.hourp t.minp t.secp t.to_ip t.wdayp t.mdayp t.ydayp t.zonet = Time.mktime(2009,12,11,3,15,59)p tp t.strftime("%Y/%m/%d %H:%M:%S")p t.strftime("%a %b %d %H:%M:%S %Z:%Y")t = Time.nowp tt.utcp trequire "date"t1 = DateTime.nowsleep(1)t2 = DateTime.nowp t2 - t1t = DateTime.nowputs tputs t + 10require "date"d = Date.todayputs d.yearputs d.monthputs d.dayputs dputs Date.new(2009,2,-1)puts d + 1puts d - 2puts d >> 3puts d << 10

?基础居多,需要多加练习。:)

读书人网 >Ruby Rails

热点推荐