国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
ruby筆記

沒有什么特別的原因開學(xué)學(xué)習(xí)Ruby,只是從興趣出發(fā)

Programming Ruby 2nd Edition

Range

轉(zhuǎn)化為數(shù)組很簡單,to_a方法搞定:r = 3..12arr = r.to_a     # [3,4,5,6,7,8,9,10,11,12]digits = 0..9digits.include?(5) -> truedigits.min -> 0digits.max -> 9digits.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..6r2 = 3...6     #....不包含上線6r1a, r1b = r1.first, r1.last    # 3, 6r1c, r1d = r1.begin, r1.end     # 3, 6r2a, r2b = r2.begin, r2.end     # 3, 6 (注意:不是3和5)r1 = "7".."9"r2 = "7".."10"r1.each {|x| puts x }   # 打印出7,8,9r2.each {|x| puts x }   # 未打印任何東西##為什么會出現(xiàn)這樣的情況?這是因為這里都是字符串,由于r1中,"7"比"9"小所以,它是個合理的Range;而表達式r2中,"7"比"10"大,下限大于了上限,就不合理了。r1 = 23456..34567x = 14142y = 31416r1.include?(x)      # falser1.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之間步長5

數(shù)字

a -> 97 #ASCII值

讀取文件

f=File.open("testfile")f.each{|line| puts line}f.close

Reading

line=gets   #從鍵盤輸入負值給lineprint line  #打印line

Hash表

histogram = Hash.new() #新建一個Hash表histogram['key1'] -> 0histogram['key1'] = histogram['key1'] + 1histogram['key1'] -> 1histogram.default = "hello"  #沒有定義的key會返回hello

Array

shift

char=[a,b,b,c].shift   #刪除數(shù)組中的第一個字符puts char  # a
[1,3,5,7,9].find{|v| v*v < 30 }   #返回滿足條件的第一個值["H", "A", "L"].collect {|x| x.succ }  #輸下當前字符的下一字符[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  #新建一個數(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ù)組長度,變量為字符串長度

變量

全局變量 用$開頭
實例變量 用@開頭
類變量 用@@ 開頭

person = "Tim"person.id -> 936870person.class -> Stringperson -> "Tim"

#{..} → 可以在引號中引用變量值或

def say_goodnight(name)result = "Good night, #{name.capitalize}" #.capitalize輸出字符串首字母大寫return resultendputs say_goodnight('uncle')輸出結(jié)果:Good night, Uncle

String

squeeze

我們可以使用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."

將一個字符串按照單詞進行反轉(zhuǎn),那么你就會用到split方法和,數(shù)組的reverse方法

phrase = "Now here's a sentence"puts phrase.split(" ").reverse.join(" ")# "sentence a here's Now"

strip

strip將會刪除掉字符串的開頭和結(jié)尾的空格sss="a d c "puts sss.strip  => abc如果你只想從字符串的開頭或者結(jié)尾的空格,那么可以使用lstrip或者rstripstr = "  abc  "s2 = str.lstrip       # "abc  "s3 = str.rstrip       # "  abc"

chop

chop方法將會刪除最后一個字符,返回新的string。如果字符是以\r\n結(jié)束,則兩個字符都會被刪除str = "abcxyz"s1 = str.chop           # "abcxy"str2="abc\r\n"s2=str2.chop  #abc

chomp

str = "abcxyz"puts s1 = str.chomp #abcxyzstr2 = "123\n"puts s2=str2.chomp   #123str1 = "abcxyz"str2 = "abcxyz"puts s1 = str1.chomp("yz")   # "abcx"puts s2 = str2.chomp("x")    # "abcxyz"   #只匹配結(jié)尾的詞

unpack

puts  "E".unpack("c")    #69   輸出一個字符串的asc碼值

?

puts  "a"<<111    <<符號或者chr來把一個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,"++")    #左對齊其余用++補齊puts str.rjust(8,"++")    #右對齊其余用++補齊
"Seconds/day: #{24*60*60}" -> Seconds/day: 86400"#{'Ho! '*3}Merry Christmas!" -> Ho! Ho! Ho! Merry Christmas!"This is line #$." -> This is line 3         # #?顯示程序塊中語句所在的行號

each_byte

str="bobo"str.each_byte{|byte| puts byte.chr}結(jié)果:bobo

dup

person1 = "Tim"person2 = person1.dup   #同.new功能相同也可以用person2=person1person1[0] = "J"person1 -> "Jim"person2 -> "Tim"

freeze

person1 = "Tim"person2 = person1person1.freeze   # 字符串不可變person2[0] = "J"結(jié)果:prog.rb:4:in `[]=': can't modify frozen string (TypeError)from prog.rb:4

index & rindex

index方法返回指定的子字符串,正則表達式或者字符的起始位置(如果有多個匹配的只返回第一個匹配的起始位置)沒有發(fā)現(xiàn)的話返回nil,而rindex則是從string的右邊(也就是結(jié)束處)開始查找,不過返回的值卻是從左邊數(shù)起的:str = "Albert Einstein"puts pos1 = str.index(?E)        # 7puts pos1 = str.index(69)        # 7puts pos2 = str.index("bert")    # 2puts pos3 = str.index(/in/)      # 8puts pos4 = str.index(?e)        # nilputs pos5 = str.index("bart")    # nilputs pos6 = str.index(/wein/)    # nil

正側(cè)表達式

sub或者gsub方法來進行替換,他們兩個方法第一個參數(shù)都是接受正則表達式。其中,sub方法替換掉第一個匹配的地方,而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ù)表

函數(shù)名稱 說明 示例
* 將字符串拷貝N次 “ha”*4 ? “hahahaha”
+ 連接字符串 “yes”+“no” ? “yesno”
? 連接字符串 “yes”+“no” ? “yesno”
concat 連接字符串 “yes”+“no” ? “yesno”
? 比較字符串,返回值: 大于=-1 “Ab” ? “ab” ? -1

等于=0 “ab” ? “ab” ? 0

小于=1 “ab” ? “Ab” ? 1
== 或 === 判斷兩個對象是否相等 “1” == “1” ? true , “1” == 1 ? flase
=~ 匹配正則表達式 “abc123abc” =~ /\d/ ? 3
[ ] 或 slice 返回字符串的某一范圍內(nèi)的值 “abc”[0,2] ? “ab”, “hello”[/llo/] ? “l(fā)lo”, “abc”.slice(0,2) ? “ab”
[]= 替換字符串的某一范圍內(nèi)的值 a=“hello word” → a[1,2]=“OO” → puts a ?“hOOlo word”
capitalize,capitalize! 把字符串的首字母大寫,其他字母小寫 “hi,Ruby”.capitalize ? “Hi,ruby”
chomp,chomp! 刪除字符串后的空白字符 “string”r”n”.chomp ?“string”
chop 刪除最后一個字符 “string”.chop ? “strin”
count 返回該字符串中含的字符個數(shù) a = “hello world” → a.count “l(fā)o” ? 5 #(l出現(xiàn) 3次,o出現(xiàn) 2次)

基礎(chǔ)

ruby打印輸出命令puts 帶換行符輸出priint 不帶換行符輸出ruby中區(qū)間表示1..5表示1,2,3,4,51...5表示1,2,3,4
/home1/yepnnet/public_html/wiki/data/attic/ruby.1223652031.txt.gz · 最后更改: 2008/10/10 09:20 由 admin
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
gets()與puts()
字符串處理函數(shù)
C語言 字符串常用函數(shù) 示例
UC頭條:[C語言—零基礎(chǔ)第十三課]字符串的奧秘
字符串的輸入和輸出
很基礎(chǔ)的結(jié)構(gòu)體里的字符數(shù)組賦值問題
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服