Upgrade to Pro — share decks privately, control downloads, hide ads and more …

第四講:控制流程、基本內建物件

Avatar for MROS MROS
October 22, 2013

 第四講:控制流程、基本內建物件

ases ruby上課講義(四)

Avatar for MROS

MROS

October 22, 2013

Other Decks in Programming

Transcript

  1. 方法的進階使用 def test(a, b=3, c=4) print "#{a} #{b} #{c}" end

    test(1, 5, 6) # 1 5 6 test(1, 5) # 1 5 4 test(1) # 1 3 4
  2. 子字串截取 a = “1234567” a[2] # “3” a[3] # “4”

    a[-1] # “7” 由後往前數 a[2..4] # “345” a[3, 2] # “45”
  3. 一些方法 a = "to tHe end of the wORld." puts

    a.capitalize puts a.upcase puts a.downcase puts a.capitalize! puts a.upcase! puts a.downcase!
  4. 陣列:創建 a = [] a = [1, 3, "dd"] a

    = Array.new a = Array.new(3) a = Array.new(3, "hihi")
  5. 陣列:添加 a = [1, 2, 3] a.unshift(0) # a now

    is [0, 1, 2, 3] a = [1, 2, 3] a.push(0) # a now is [1, 2, 3, 0] a = [1, 2, 3] a << 0 # a now is [1, 2, 3, 0] 以上僅有<<只能接一個參數
  6. 陣列:刪去 a = [1, 2, 3] a.pop # now a

    is [1, 2] a = [1, 2, 3] a.shift #now a is [2, 3]
  7. 陣列裡的陣列 a = [[1, 2], 43, "pop", [8, [4]] a[0]

    == [1, 2] a[0][0] == 1 a[3][1] == [4] a[3][1][0] == 4