003 Ruby小觀念與小技巧Part2
-
Upload
bruce-li -
Category
Technology
-
view
1.089 -
download
9
description
Transcript of 003 Ruby小觀念與小技巧Part2
![Page 1: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/1.jpg)
30分鐘技術分享Ruby小觀念&技巧 Part 2 By Bruce Li
2012/04/19雲端線上科技股份有限公司
12年4月21日星期六
![Page 2: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/2.jpg)
這次要講三個東西
• require, load, include
• yield
• method的參數
12年4月21日星期六
![Page 3: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/3.jpg)
require, load, include
12年4月21日星期六
![Page 4: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/4.jpg)
require vs. load
12年4月21日星期六
![Page 5: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/5.jpg)
require vs. load跳過
12年4月21日星期六
![Page 6: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/6.jpg)
require vs. load跳過
值都⼀一樣
12年4月21日星期六
![Page 7: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/7.jpg)
require vs. load
又load了⼀一次
跳過
值都⼀一樣
12年4月21日星期六
![Page 8: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/8.jpg)
require vs. load
值很可能不同
又load了⼀一次
跳過
值都⼀一樣
12年4月21日星期六
![Page 9: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/9.jpg)
關於require/load的參數
• require跟load都是吃字串
• load必須提供附檔名,require則可忽略
• 自己寫的檔案不指定目錄的話,都會掛
12年4月21日星期六
![Page 10: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/10.jpg)
解決相對目錄問題
/Users/bruce/Desktop/var.rb
/Users/bruce/Desktop/../var.rb= /Users/bruce/var.rb
指明絕對路徑
12年4月21日星期六
![Page 11: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/11.jpg)
解決相對目錄問題指明絕對路徑
12年4月21日星期六
![Page 12: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/12.jpg)
require vs. include
module跟class寫在同⼀一個檔案當然也是可以include的這樣就不需先要require
12年4月21日星期六
![Page 13: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/13.jpg)
補充:這樣也是可以的
12年4月21日星期六
![Page 14: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/14.jpg)
include lib下的module (rails)
• rails 2的時候是可以直接include的
• rails 3的時候因為安全性考量,include前需要先require
• require "authenticated_system"=> require RailsRoot/lib/authenticated_system.rb
12年4月21日星期六
![Page 15: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/15.jpg)
module命名名詞
形容詞
非強制,不過這樣還蠻有道理的12年4月21日星期六
![Page 16: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/16.jpg)
yield
12年4月21日星期六
![Page 17: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/17.jpg)
用來測效能
=> 1.2011590003967285
12年4月21日星期六
![Page 18: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/18.jpg)
yield的參數
12年4月21日星期六
![Page 19: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/19.jpg)
yield的參數
246
12年4月21日星期六
![Page 20: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/20.jpg)
yield的參數
246
12年4月21日星期六
![Page 21: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/21.jpg)
method的參數
12年4月21日星期六
![Page 22: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/22.jpg)
選擇性參數/預設值
12年4月21日星期六
![Page 23: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/23.jpg)
不定數量參數
12年4月21日星期六
![Page 24: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/24.jpg)
不定數量參數
12年4月21日星期六
![Page 25: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/25.jpg)
類似的:hash
12年4月21日星期六
![Page 26: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/26.jpg)
混用的順序
12年4月21日星期六
![Page 27: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/27.jpg)
混用的順序 - hash
√
省略{ }的hash要在固定參數之後要不然就明確的指定 { }
12年4月21日星期六
![Page 28: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/28.jpg)
end
12年4月21日星期六
![Page 29: 003 Ruby小觀念與小技巧Part2](https://reader036.fdocuments.net/reader036/viewer/2022081716/53ffceba8d7f7261088b47d5/html5/thumbnails/29.jpg)
廣告雲端線上徵Rails/iOS/Android工程師中
‣ inside job board
我的技術&生活雜事部落格
‣ http://littlebmix.blogspot.com/
學習成功部落格
‣ http://ascend-er.blogspot.com/
12年4月21日星期六