整數(shù)有用的迭代器 3.times { print "X " } => X X X 1.upto(5) { |i| print i, " " } =>1 2 3 4 5 99.downto(95) { |i| print i, " " }=>99 98 97 96 9550.step(80, 5) { |i| print i, " " }=>50 55 60 65 70 75 80
二、字符串
Ruby的字符串是8位字節(jié)的簡(jiǎn)單序列,字符串是String類的對(duì)象
注意轉(zhuǎn)換機(jī)制(注意單引號(hào)與雙引號(hào)的區(qū)別),如: 單引號(hào)中兩個(gè)相連的反斜線被替換成一個(gè)反斜線,,一個(gè)反斜線后跟一個(gè)單引號(hào)被替換成一個(gè)單引號(hào) 'escape using "//"' >> 轉(zhuǎn)義為"/" 'That/'s right' >> That's right
雙引號(hào)支持多義的轉(zhuǎn)義 "/n" #{expr}序列來替代任何的Ruby表達(dá)式的值 ,(全局變量、類變量或者實(shí)例變量,那么可以省略大括號(hào)) "Seconds/day: #{24*60*60}" >> Seconds/day: 86400 "#{'Ho! '*3}Merry Christmas" >> Ho! Ho! Ho! Merry Christmas "This is line #$." >> This is line 3
here document來創(chuàng)建一個(gè)字符串,end_of_string 為結(jié)束符號(hào) aString = <<END_OF_STRINGThe body of the stringis the input lines up toone ending with the same text that followed the '<<'END_OF_STRING
%q和%Q分別把字符串分隔成單引號(hào)和雙引號(hào)字符串(即%q與%Q后面的符號(hào)具有',"的功能) %q/general single-quoted string/ >> general single-quoted string
用Regxp#match(aString)的形式或者匹配運(yùn)算符=~(正匹配)和!~(負(fù)匹配)來匹配字符串了。匹配運(yùn)算符在String和Regexp中都有定義,如果兩個(gè)操作數(shù)都是字符串,則右邊的那個(gè)要被轉(zhuǎn)換成正則表達(dá)式 exp: a = "Fats Waller" a =~ /a/ >> 1 a =~ /z/ >> nil a =~ "ll" >> 7
上面返回的是匹配字符的位置,其它 $&接受被模式匹配到的字符串部分 $`接受匹配之前的字符串部分 $'接受之后的字符串。 exp:下面的方法后繼都會(huì)用到 def showRE(a,re) if a =~ re "#{$`}<<#{$&}>>#{$'}" #返回前、中、后 else "no match" end end
字符類縮寫 序列 形如 [ ... ] 含義 /d [0-9] Digit character /D [^0-9] Nondigit /s [/s/t/r/n/f] Whitespace character 匹配一個(gè)單空白符 /S [^/s/t/r/n/f] Nonwhitespace character /w [A-Za-z0-9_] Word character /W [^A-Za-z0-9_] Nonword character
重復(fù) r * 匹配0個(gè)或多個(gè)r的出現(xiàn) r + 匹配一個(gè)或多個(gè)r的出現(xiàn) r ? 匹配0個(gè)或1個(gè)r的出現(xiàn) r {m,n} 匹配最少m最多n個(gè)r的出現(xiàn) r {m,} 匹配最少m個(gè)r的出現(xiàn)
重復(fù)結(jié)構(gòu)有高優(yōu)先權(quán):即它們僅和模式中的直接正則表達(dá)式前驅(qū)捆綁 /ab+/匹配一個(gè)"a"后跟一個(gè)活著多個(gè)"b",而不是"ab"的序列 /a*/會(huì)匹配任何字符串:0個(gè)或者多個(gè)"a"的任意字符串。 exp: a = "The moon is made of cheese" showRE(a, //w+/) >> <<The>> moon is made of cheese showRE(a, //s.*/s/) >> The<< moon is made of >>cheese showRE(a, //s.*?/s/) >> The<< moon >>is made of cheese showRE(a, /[aeiou]{2,99}/) >> The m<<oo>>n is made of cheese showRE(a, /mo?o/) >> The <<moo>>n is made of cheese
替換 "|"既匹配它前面的正則表達(dá)式或者匹配后面的 a = "red ball blue sky" showRE(a, /d|e/) >> r<<e>>d ball blue sky showRE(a, /al|lu/) >> red b<<al>>l blue sky showRE(a, /red ball|angry sky/) >> <<red ball>> blue sky
分組 圓括號(hào)把正則表達(dá)式分組,組中的內(nèi)容被當(dāng)作一個(gè)單獨(dú)的正則表達(dá)式 showRE('banana', /(an)+/) >> b<<anan>>a # 匹配重復(fù)的字母 showRE('He said "Hello"', /(/w)/1/) >> He said "He<<ll>>o" # 匹配重復(fù)的子字符串 showRE('Mississippi', /(/w+)/1/) >> M<<ississ>>ippi
基于模式的替換 你是否想過,大小寫替換。 方法String#sub和String#gsub都在字符串中搜索匹配第一個(gè)參數(shù)的部分,然后用第二個(gè)參數(shù)來替換它們。String#sub只替換一次,而String#gsub替換所有找到的匹配。都返回一個(gè)包含了替換的新的字符串的拷貝。進(jìn)化版本是String#sub!和 String#gsub! a = "the quick brown fox" a.sub(/[aeiou]/, '*') >> "th* quick brown fox" a.gsub(/[aeiou]/, '*') >> "th* q**ck br*wn f*x" a.sub(//s/S+/, '') >> "the brown fox" a.gsub(//s/S+/, '') >> "the" 第二個(gè)參數(shù)可以是代碼塊 a = "the quick brown fox" a.sub (/^./) { $&.upcase } >> "The quick brown fox" a.gsub(/[aeiou]/) { $&.upcase } >> "thE qUIck brOwn fOx"