DAY 13
5
Modern Web

# 認識 irb

### 進入 irb

``````D:\只要有心，人人都可以作卡米狗>
``````

``````D:\只要有心，人人都可以作卡米狗>irb
irb(main):001:0>
``````

### 離開 irb

``````D:\只要有心，人人都可以作卡米狗>irb
irb(main):001:0> exit

D:\只要有心，人人都可以作卡米狗>
``````

# 認識資料型態

### 數字

``````D:\只要有心，人人都可以作卡米狗>irb
irb(main):001:0> 5566
=> 5566
``````

``````irb(main):002:0> 5566+9527
=> 15093
``````

``````irb(main):003:0> 5566.class
=> Integer
``````

5566 是 Integer(整數)。

### 字串

``````irb(main):004:0> '5566'.class
=> String
``````

``````irb(main):005:0> '5566+9527'
=> "5566+9527"
``````

``````irb(main):006:0> '5566'+'9527'
=> "55669527"
``````

### 變數

``````irb(main):007:0> idol = 5566
=> 5566
``````

``````irb(main):008:0> idol
=> 5566
``````

### 陣列

``````irb(main):009:0> eat = ["Mcdonald's", 'KFC', 'BUG King', 'MOS BUG']
=> ["Mcdonald's", "KFC", "BUG King", "MOS BUG"]
``````

• eat[0] = "Mcdonald's"
• eat[1] = "KFC"
• eat[2] = "BUG King"
• eat[3] = "MOS BUG"

``````irb(main):010:0> eat = []
=> []
irb(main):011:0> eat[0] = "Mcdonald's"
=> "Mcdonald's"
irb(main):012:0> eat[1] = 'KFC'
=> "KFC"
irb(main):013:0> eat[2] = 'BUG King'
=> "BUG King"
irb(main):014:0> eat[3] = 'MOS BUG'
=> "MOS BUG"
irb(main):015:0> eat
=> ["Mcdonald's", "KFC", "BUG King", "MOS BUG"]
``````

``````irb(main):016:0> eat = []
=> []
irb(main):017:0> eat << "Mcdonald's"
=> ["Mcdonald's"]
irb(main):018:0> eat << 'KFC'
=> ["Mcdonald's", "KFC"]
irb(main):019:0> eat << 'BUG King'
=> ["Mcdonald's", "KFC", "BUG King"]
irb(main):020:0> eat << 'MOS BUG'
=> ["Mcdonald's", "KFC", "BUG King", "MOS BUG"]
``````

`<<` 表示放一個東西進入陣列。

``````irb(main):021:0> eat.class
=> Array
``````

``````irb(main):022:0> eat[0].class
=> String
``````

``````irb(main):023:0> eat[2]
=> "BUG King"
``````

``````irb(main):024:0> bgk = 2
=> 2
irb(main):025:0> eat[bgk]
=> "BUG King"
``````

# 雜湊陣列

``````irb(main):026:0> eat_hash = {}
=> {}
irb(main):027:0> eat_hash[0] = "Mcdonald's"
=> "Mcdonald's"
irb(main):028:0> eat_hash[1] = "KFC"
=> "KFC"
irb(main):029:0> eat_hash[2] = "BUG King"
=> "BUG King"
irb(main):030:0> eat_hash[3] = "MOS BUG"
=> "MOS BUG"
irb(main):031:0> eat_hash
=> {0=>"Mcdonald's", 1=>"KFC", 2=>"BUG King", 3=>"MOS BUG"}
``````

``````irb(main):032:0> eat_hash = {0=>"Mcdonald's", 1=>"KFC", 2=>"BUG King", 3=>"MOS BUG"}
=> {0=>"Mcdonald's", 1=>"KFC", 2=>"BUG King", 3=>"MOS BUG"}
``````

``````irb(main):033:0> kamigo = {}
=> {}
irb(main):034:0> kamigo['Q'] = 'A'
=> "A"
``````

`kamigo[關鍵字] = 要回應的內容`