沒有什么特別的原因開學(xué)學(xué)習(xí)Ruby,,只是從興趣出發(fā) Programming Ruby 2nd EditionRange轉(zhuǎn)化為數(shù)組 很簡(jiǎn)單,to_a方法搞定: r = 3..12 arr = r.to_a # [3,4,5,6,7,8,9,10,11,12] digits = 0..9 digits.include?(5) -> true digits.min -> 0 digits.max -> 9 digits.reject {|i| i < 5 } -> [5, 6, 7, 8, 9] digits.each {|digit| dial(digit) } -> 0..9 ('a'..'z').to_a.each{|i| puts i} #顯示a到z的字符串 r1 = 3..6 r2 = 3...6 #....不包含上線6 r1a, r1b = r1.first, r1.last # 3, 6 r1c, r1d = r1.begin, r1.end # 3, 6 r2a, r2b = r2.begin, r2.end # 3, 6 (注意:不是3和5) r1 = "7".."9" r2 = "7".."10" r1.each {|x| puts x } # 打印出7,8,9 r2.each {|x| puts x } # 未打印任何東西 ##為什么會(huì)出現(xiàn)這樣的情況,?這是因?yàn)檫@里都是字符串,,由于r1中,,"7"比"9"小 所以,,它是個(gè)合理的Range,;而表達(dá)式r2中,"7"比"10"大,,下限大于了上限,,就不合理了。 r1 = 23456..34567 x = 14142 y = 31416 r1.include?(x) # false r1.include?(y) # true 迭代器3.times { print "X " } 1.upto(5) {|i| print i, " " } 99.downto(95) {|i| print i, " " } 50.step(80, 5) {|i| print i, " " } #50至80之間步長(zhǎng)5 數(shù)字a -> 97 #ASCII值 讀取文件f=File.open("testfile") f.each{|line| puts line} f.close Readingline=gets #從鍵盤輸入負(fù)值給line print line #打印line Hash表histogram = Hash.new() #新建一個(gè)Hash表 histogram['key1'] -> 0 histogram['key1'] = histogram['key1'] + 1 histogram['key1'] -> 1 histogram.default = "hello" #沒有定義的key會(huì)返回hello Arrayshift char=[a,b,b,c].shift #刪除數(shù)組中的第一個(gè)字符 puts char # a [1,3,5,7,9].find{|v| v*v < 30 } #返回滿足條件的第一個(gè)值 ["H", "A", "L"].collect {|x| x.succ } #輸下當(dāng)前字符的下一字符 [1,3,5,7,9].each{|i| puts i } #遍歷數(shù)組 [1,3,5,7].inject {|sum, element| sum+element} #數(shù)組求和 [1,3,5,7].inject {|product, element| product*element} #數(shù)組求積 a=Array.new #新建一個(gè)數(shù)組 a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ] a[0] -> "ant" a[3] -> "dog" # this is the same: a = %w{ ant bee cat dog elk } a[0] -> "ant" a[3] -> "dog" a.length -> 5 #數(shù)組長(zhǎng)度,,變量為字符串長(zhǎng)度 變量全局變量 用$開頭 person = "Tim" person.id -> 936870 person.class -> String person -> "Tim" #{..} → 可以在引號(hào)中引用變量值或 def say_goodnight(name) result = "Good night, #{name.capitalize}" #.capitalize輸出字符串首字母大寫 return result end puts say_goodnight('uncle') 輸出結(jié)果: Good night, Uncle Stringsqueeze 我們可以使用squeeze 方法來一處重復(fù)的字符 s1 = "bookkeeper" puts s2 = s1.squeeze # "bokeper" s3 = "Hello..." puts s4 = s3.squeeze # "Helo." #If a parameter is specified, only those characters will be squeezed. puts s5 = s3.squeeze(".") # "Hello." 將一個(gè)字符串按照單詞進(jìn)行反轉(zhuǎn),,那么你就會(huì)用到split方法和,數(shù)組的reverse方法 phrase = "Now here's a sentence" puts phrase.split(" ").reverse.join(" ") # "sentence a here's Now" strip strip將會(huì)刪除掉字符串的開頭和結(jié)尾的空格 sss="a d c " puts sss.strip => abc 如果你只想從字符串的開頭或者結(jié)尾的空格,,那么可以使用lstrip或者rstrip str = " abc " s2 = str.lstrip # "abc " s3 = str.rstrip # " abc" chop chop方法將會(huì)刪除最后一個(gè)字符,,返回新的string。如果字符是以\r\n結(jié)束,,則兩個(gè)字符都會(huì)被刪除 str = "abcxyz" s1 = str.chop # "abcxy" str2="abc\r\n" s2=str2.chop #abc chomp str = "abcxyz" puts s1 = str.chomp #abcxyz str2 = "123\n" puts s2=str2.chomp #123 str1 = "abcxyz" str2 = "abcxyz" puts s1 = str1.chomp("yz") # "abcx" puts s2 = str2.chomp("x") # "abcxyz" #只匹配結(jié)尾的詞 unpack puts "E".unpack("c") #69 輸出一個(gè)字符串的asc碼值 ? puts "a"<<111 <<符號(hào)或者chr來把一個(gè)asc轉(zhuǎn)換為字符串: puts 111.chr scan "hello world".scan(/./){|s| print s} #掃描匹配的字符串并打印 casecmp n4 = "ABC".casecmp("abc") # 0 功能同<=>忽略大小寫 puts str="bobo" puts str.ljust(8,"++") #左對(duì)齊其余用++補(bǔ)齊 puts str.rjust(8,"++") #右對(duì)齊其余用++補(bǔ)齊 "Seconds/day: #{24*60*60}" -> Seconds/day: 86400 "#{'Ho! '*3}Merry Christmas!" -> Ho! Ho! Ho! Merry Christmas! "This is line #$." -> This is line 3 # #?顯示程序塊中語(yǔ)句所在的行號(hào) each_byte str="bobo" str.each_byte{|byte| puts byte.chr} 結(jié)果: b o b o dup person1 = "Tim" person2 = person1.dup #同.new功能相同也可以用person2=person1 person1[0] = "J" person1 -> "Jim" person2 -> "Tim" freeze person1 = "Tim" person2 = person1 person1.freeze # 字符串不可變 person2[0] = "J" 結(jié)果: prog.rb:4:in `[]=': can't modify frozen string (TypeError) from prog.rb:4 index & rindex index方法返回指定的子字符串,,正則表達(dá)式或者字符的起始位置(如果有多個(gè)匹配的只返回第一個(gè)匹配的起始位置) 沒有發(fā)現(xiàn)的話返回nil,而rindex則是從string的右邊(也就是結(jié)束處)開始查找,不過返回的值卻是從左邊數(shù)起的: str = "Albert Einstein" puts pos1 = str.index(?E) # 7 puts pos1 = str.index(69) # 7 puts pos2 = str.index("bert") # 2 puts pos3 = str.index(/in/) # 8 puts pos4 = str.index(?e) # nil puts pos5 = str.index("bart") # nil puts pos6 = str.index(/wein/) # nil 正側(cè)表達(dá)式 sub或者gsub方法來進(jìn)行替換,,他們兩個(gè)方法第一個(gè)參數(shù)都是接受正則表達(dá)式,。 其中,sub方法替換掉第一個(gè)匹配的地方,,而gsub方法替換掉全部匹配的地方: s1 = "spam, spam, and eggs" s2 = s1.sub(/spam/,"bacon") # "bacon, spam, and eggs" s3 = s2.sub(/(\w+), (\w+),/,'\2, \1,') # "spam, bacon, and eggs" s4 = "Don't forget the spam." s5 = s4.sub(/spam/) { |m| m.reverse } # "Don't forget the maps." 把匹配部分的單詞反轉(zhuǎn) s5 = "alfalfa abracadabra" s6 = s5.gsub(/a[bl]/,"xx") # "xxfxxfa xxracadxxra" s5.gsub(/[lfdbr]/) { |m| m.upcase + "-" } # s5 is now "aL-F-aL-F-a aB-R-acaD-aB-R-a" String類常用函數(shù)表
基礎(chǔ)ruby打印輸出命令 puts 帶換行符輸出 priint 不帶換行符輸出 ruby中區(qū)間表示 1..5表示1,2,,3,,4,5 1...5表示1,,2,,3,4 /home1/yepnnet/public_html/wiki/data/attic/ruby.1223652031.txt.gz · 最后更改: 2008/10/10 09:20 由 admin |
|