標籤:
1.irb參數配置
~/.irbrc
IRB.conf[:PROMPT_MODE] = :SIMPLE #簡化 irb 提示符,以及禁用一些煩人的自動縮排行為IRB.conf[:AUTO_INDENT_MODE] = false
2.注釋:
#單行
=begin
......多行
=end
3.字串
字串串連
>> "jk" + "jk"=> "jkjk"
字串插值
>> one = "foo"=> "foo">> "#{one} is one"=> "foo is one"
字串列印
>> puts "jk" #列印後內建一個分行符號jk=> nil>> p "jk" #列印出和原來一樣的"jk"=> "jk">> print "jk" #列印不換行jk=> nil
單雙引號字串
>> ‘jk\n#{one}‘ #不進行轉移字元和插值操作=> "jk\\n\#{one}">> "jk\n#{one}" #輸出轉義後的字元和插值操作=> "jk\nfoo"
對象和訊息傳送
>> "jk".length=> 2>> "".empty?=> true>> "jk".nil?=> false>> "jk".include?("j")=> true
判斷的時候可以配合 &&(和) || (或) !(非)來操作
>> one = "foo"
=> "foo"
>> two = ""=> "">> if one.empty? && two.empty?>> print "both empty">> elsif one.empty? || two.empty?>> print "empty exist">> elsif !x.empty?>> print "x is not empty">> endempty exist=> nil
to_s可以把任何對象轉換成字串
>> nil.to_s.empty?=> true
unless判斷
>> two = "dasd"=> "dasd">> print "ss" unless two.empty?ss=> nil
使用!!可以兩次取反,把對象轉換為布爾類型
>> !!nil #nil對象為布爾假=> false>> !!0 #其他都為真=> true
數值和範圍
split方法,將字串分割成想要的字元數組
>> "dasjkd das d dasd".split #預設去掉空格=> ["dasjkd", "das", "d", "dasd"]>> "fdjaskl,jfdskl,jfklasd".split(",") #設定用,分割=> ["fdjaskl", "jfdskl", "jfklasd"]
數組的索引
>> a=[12,34,45,67,89,8]=> [12, 34, 45, 67, 89, 8]>> a[0]=> 12>> a[2]=> 45>> a[-4] #索引可是負數=> 45
>> a.fifth #可以這樣尋找,但這是rails添加的效果
=> 89
>> a.first
=> 12
>> a.last
=> 8
>> a=[12,34,45,67,89,08] #注意數字之前不能有0,提示錯誤無效的十進位
SyntaxError: (irb):40: Invalid octal digit
a=[12,34,45,67,89,08]
^
數組的一系列方法
>> a.length=> 6>> a.include?(45)=> true>> a.sort #按有小到大順序排列(不改變原有數組)=> [8, 12, 34, 45, 67, 89] >> a.reverse #順序反過來=> [8, 89, 67, 45, 34, 12]
>> a.shuffle #隨意打亂順序
=> [45, 12, 8, 34, 67, 89]
>> a.sort! #按有小到大順序排列 (改變原有數組)
=> [8, 12, 34, 45, 67, 89]
>> a.push(4) #添加元素 (改變原有數組)
=> [8, 12, 34, 45, 67, 89, 4]
>> a << 3 #添加元素(改變原有數組)
=> [8, 12, 34, 45, 67, 89, 4, 3]
>> a << "tu" << "re" #串聯添加,並且數組中可以添加其它類型的元素
=> [8, 12, 34, 45, 67, 89, 4, 3, "tu", "re"]
>> a.join #將字串合并
=> "8123445678943ture"
>> a.join(",") #添加串連符
=> "8,12,34,45,67,89,4,3,tu,re"
範圍操作
>> (0...9).to_a #去掉最後的一個值,轉化為數組
=> [0, 1, 2, 3, 4, 5, 6, 7, 8]
>> (0..9).to_a #保留所有至
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>> a=(0..9).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> a[4..(a.length)] #使用length方法
=> [4, 5, 6, 7, 8, 9]
>> a[4..-2] #小技巧
=> [4, 5, 6, 7, 8]
塊
>> (1..9).each do |f|?> print f*f>> end149162536496481
>> (1..9).map {|i| i*i} #將每個元素變換並返回map
=> [1, 4, 9, 16, 25, 36, 49, 64, 81]
>> (1..9).reduce {|i| i*i} #將整個數組變換為一個值返回
=> 1
>> %w{das das das} #%w將元素變換為字串返回
=> ["das", "das", "das"]
雜湊和符號
>> user = {} #初始化雜湊
=> {}
>> user["one"] = "yz00"
=> "yz00"
>> user["two"] = "yz01"
=> "yz01">> user["one"] #擷取雜湊
=> "yz00"
>> user
=> {"one"=>"yz00", "two"=>"yz01"}>> user = {"one" => "yz00","two" => "yz01"} #也可以這樣賦值
=> {"one"=>"yz00", "two"=>"yz01"} #注意在逗號後面有一個空格,約定俗成的
可以把符號看成沒有約束的字串,所以可以寫成下面的形式
>> user = {:one => "yz00", :two => "yz01"} #不用初始化=> {:one=>"yz00", :two=>"yz01"}
由於符號當作鍵用的多,1.9後這樣定義
>> user = {one: "yz00", two: "yz01"}
=> {:one=>"yz00", :two=>"yz01"}
嵌套雜湊
>> users[:user] = user
=> {:one=>"yz00", :two=>"yz01"}
>> users
=> {:user=>{:one=>"yz00", :two=>"yz01"}}
>> users[:user][:one]
=> "yz00"
>> print (1..5).to_a
[1, 2, 3, 4, 5]=> nil
>> puts (1..5).to_a
1
2
3
4
5
=> nil
>> puts (1..5).to_a.inspect #inspect方法輸出數組的字面形式,輸出對象時相當於P方法
[1, 2, 3, 4, 5]
=> nil
>> p (1..5).to_a
[1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
ruby日記1