SlideShare a Scribd company logo
Ruby 使用手冊
Part 1
Compass Study Group
Jian-Long Huang
2016.05.03
甚麼是 Ruby?
• 直譯式腳本語言 (interpreted scripting language):
 能夠直接產生作業系統呼叫
 強大的字串處理 (string operations) 及正規表示式 (regular expressions)
 在開發時提供即時回饋
• 快捷易用:
 不需要變數宣告 (variable declarations)
 變數不需要型別 (typed)
 語法簡單一致
 自動管理記憶體
2
甚麼是 Ruby?
• 物件導向程式設計:
 任何東西都是物件
 類別、方法、繼承等
 單件方法 (singleton methods)
 模組提供「混入」(mixin) 功能
 迭代器 (iterators) 及閉包 (closures)
• 以及:
 多精確度整數 (multiple precision integers)
 方便的例外處理 (exception processing)
 動態載入 (dynamic loading)
 執行緒支援 (threading support)
3
起步走
• 顯示 Ruby 版本
• 執行 Ruby 命令
• 執行 Ruby 檔
$ ruby –v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
$ ruby -e 'puts "hello world"'
hello world
$ echo "puts 'hello world'" > hello.rb
$ ruby hello.rb
hello world
4
安裝 Ruby
• Windows
 RubyInstaller for Windows
• Linux (Ubuntu)
• Linux (CentOS)
• Mac OS X
• 更多參考 https://www.ruby-lang.org/zh_tw/documentation/installation/
$ sudo apt-get install ruby
$ sudo yum install ruby
$ brew install ruby
5
簡單示範
• 計算階乘
# 計算出數字的階乘
# 請把程式儲存為 fact.rb
def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end
puts fact(ARGV[0].to_i)
6
簡單示範
• ARGV: 命令列引數
• to_i: 將字元字串 (character string) 轉換為整數
• 不加 return,回傳值即為最後一個 statement 的傳回值。
7
互動模式
• 執行 ruby 而未輸入引數,Ruby 會從標準輸入 (standard input) 讀取指令,在輸
入結束後 (送出 Ctrl-D 指令) 開始執行。
• 輸入 irb 指令,可以進入互動模式。
8
字串 (String)
• 使用雙引號或單引號括起
• 雙引號可以使用字串內插 (string interpolation)
• 雙引號可以使用反斜線轉義字符
a = "foo"
a = 'bar'
a = "foo"
b = "#{a}bar" # ==> "foobar"
a = "foon" # ==> 輸出 foo + 換行符號
9
字串 (String)
• 串接字串
• 重複字串
• 提取字串
a = "foo" + "bar" # ==> "foobar"
a = "foo" * 2 # ==> "foofoo"
a = "parsley"
a[0] # ==> "p"
a[-1] # ==> "y"
a[0,1] # ==> "p"
a[-2,2] # ==> "ey"
a[0..3] # ==> "pars"
a[0...3] # ==> "par"
a[-5..-2] # ==> "rsle"
10
字串 (String)
• 字串比對
"foo" == "foo" # ==> true
"foo" == "bar" # ==> false
11
正規表示式
• 和 Perl 一樣,Ruby 使用 string =~ /<pattern>
 發現符合,傳回字串中符合之位置
 沒有符合,傳回 nil
• 更多參考 http://ruby-doc.org/core-2.1.1/Regexp.html
# 抓出手機號碼
phone = "123-456-7890"
if phone =~ /(d{3})-(d{3})-(d{4})/
ext = $1
city = $2
num = $3
end
12
陣列 (Array)
• 使用中括號 ([]) 建立陣列,元素可不同型態
• 陣列串接
• 陣列重複
• 使用 join 轉成字串
• 字串使用 split 轉成陣列
ary = [1, 2, "3"]
ary + ["foo", "bar"] # ==> [1, 2, "3", "foo", "bar"]
ary * 2 # ==> [1, 2, "3", 1, 2, "3"]
ary.join(":") # ==> "1:2:3"
"1:2:3".split(":") # ==> ["1", "2", "3"]
13
雜湊 (Hash)
• 使用大括號 ({}) 建立雜湊,key 和 value 可為不同型態
h = {1 => 2, "2" => "4"} # ==> {1=>2, "2"=>"4"}
# 使用 symbol 當作 key,有兩種寫法
h = {:foo => 1, :bar => 2}
h = {foo: 1, bar: 2}
14
字串符號 (Symbol)
• 一個 Symbol 物件皆為冒號開頭,例如 :foo 或 :foo_bar。它的作用就是代表一
個名字,最大的作用就是當做 Hash 的鍵 (key)。例如 { :foo => 1, :bar =>
2 },在這裡我們只需要一個識別的名字,不需要使用字串物件。
• 使用 Symbol 寫法更簡潔和獲得效能上的好處。
• 更多參考 http://kaochenlong.com/2016/04/25/string-and-symbol/
15
Percent Literals (Percent Strings)
• http://bearsu.logdown.com/posts/304719-ruby-percent-literals
16
Ruby 是完全物件導向
• 在 Ruby,每樣東西都是物件,使用 if、while 等條件判斷時,空字串、空陣列、
空雜湊都視為 true。
17
繼續簡單示範
• 猜字程式
01 words = ['foobar', 'baz', 'quux']
02 secret = words[rand(3)]
03
04 print "guess?"
05 while guess = STDIN.gets
06 guess.chop!
07 if guess == secret
08 puts "You win!"
09 break
10 else
11 puts "Sorry, you lose."
12 end
13 print "guess?"
14 end
15 puts "the word is "+ secret + " . "
18
繼續簡單示範
• rand(3): 隨機傳回 0, 1, 2 其中一個數值
• STDIN.gets 或 gets:收集使用者輸入的一行內容,直到使用者輸入 Ctrl-D
• chop: 刪除字串的最後一個字元並回傳結果
• chop!: 同 chop,但會永久改變物件本身
19
繼續簡單示範
• 正規表示式
01 st = "033[7m"
02 en = "033[m"
03
04 puts "Enter an empty string at any time to exit."
05
06 while true
07 print "str> "; STDOUT.flush; str=gets.chop
08 break if str.empty?
09 print "pat> "; STDOUT.flush; pat=gets.chop
10 break if pat.empty?
11 re = Regexp.new(pat)
12 puts str.gsub(re, "#{st}&#{en}")
13 end
20
繼續簡單示範
• empty?: 在定義方法的時候名稱可以是任何符號,一般來說結尾加上 ? 表示只會
傳回 Boolean 值,是 Ruby 的慣例。
• gsub: globally substitute 的縮寫,將匹配的內容取代成另外一個字串。
 &: 上個匹配的對象 (last match)
21
控制結構
• case
i=8
case i
when 1, 2..5
puts "1..5"
when 6..10
puts "6..10"
when 'aaa', 'bbb'
puts "aaa or bbb"
when /def/ # 正規表示式
puts "includes /def/"
end
22
控制結構
• while
i = 0
while i < 3
puts i += 1
end
# 可以簡寫成
puts i += 1 while i < 3
23
控制結構
• if
i = 0
if i == 0
puts "It's zero."
end
# 可以簡寫成
puts "It's zero" if i == 0
24
控制結構
• unless: if 的否定型
• until: while 的否定型
• 何時用 if,何時用 unless?
 一般來說,儘量使用正向表達,少用 ! 符號
# Good
puts "It's nil" if foo.nil?
puts "It's not nil" unless foo.nil?
# Not good
puts "It's not nil" if !foo.nil?
25
控制結構
• 中斷迴圈
 break: 中斷所有迴圈
 next: 中斷目前迴圈,從下個迴圈開始
 redo: 重新執行目前迴圈
 return: 中斷迴圈並跳出迴圈所在的方法
• for
for num in (4..6)
puts num
end
for elt in [100, -9.6, "pickle"]
puts elt
end
26
控制結構
• for 的另一種寫法
# 如果你習慣用 C 或 Java,你可能會比較喜歡這個。
for element in collection
...
end
# Smalltalk 程式員則可能喜歡這個。
collection.each { |element|
...
}
collection.each do |element|
...
end
27
迭代器 (Iterator)
• 字串常用的迭代器
 each_byte: 取出每個字元
 each_line: 取出每一行
• 定義迭代器:
 在 function 使用 yield ,該 function 就變成迭代器。
 yield 可將控制移至傳遞往迭代器的程式碼區塊 (block of code)。
def repeat(num)
while num > 0
yield
num -= 1
end
end
repeat(3) { puts "foo" }
28

More Related Content

What's hot

C++基礎程式設計 ch3 條件判斷
C++基礎程式設計 ch3 條件判斷C++基礎程式設計 ch3 條件判斷
C++基礎程式設計 ch3 條件判斷
requiemformemories
 
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
Justin Lin
 
看似比較簡單堆推坑教學 C語言崩潰到崩潰(一)
看似比較簡單堆推坑教學 C語言崩潰到崩潰(一)看似比較簡單堆推坑教學 C語言崩潰到崩潰(一)
看似比較簡單堆推坑教學 C語言崩潰到崩潰(一)
永立 連
 
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)
永立 連
 
C++基礎程式設計 ch2 變數與運算子
C++基礎程式設計 ch2 變數與運算子C++基礎程式設計 ch2 變數與運算子
C++基礎程式設計 ch2 變數與運算子
requiemformemories
 
Java 開發者的函數式程式設計
Java 開發者的函數式程式設計Java 開發者的函數式程式設計
Java 開發者的函數式程式設計
Justin Lin
 
Compiler for Dummy 一點都不深入的了解 Compiler, Interpreter 和 VM
Compiler for Dummy 一點都不深入的了解 Compiler, Interpreter 和 VMCompiler for Dummy 一點都不深入的了解 Compiler, Interpreter 和 VM
Compiler for Dummy 一點都不深入的了解 Compiler, Interpreter 和 VM
Li Hsuan Hung
 
5, sed
5, sed5, sed
5, sed
ted-xu
 
Ihome inaction 篇外篇之fp介绍
Ihome inaction 篇外篇之fp介绍Ihome inaction 篇外篇之fp介绍
Ihome inaction 篇外篇之fp介绍
dennis zhuang
 
Javascript 培训第三节 基础下
Javascript 培训第三节 基础下Javascript 培训第三节 基础下
Javascript 培训第三节 基础下liziqi7
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1
Sheng-Han Su
 
compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹
fdgkhdkgh tommy
 
C++基礎程式設計 ch5 陣列
C++基礎程式設計 ch5 陣列C++基礎程式設計 ch5 陣列
C++基礎程式設計 ch5 陣列
requiemformemories
 
Wasm text
Wasm textWasm text
Wasm text
Sun bingxin
 
5, initialization & cleanup
5, initialization & cleanup5, initialization & cleanup
5, initialization & cleanup
ted-xu
 
改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法crasysatan
 
Ptyhon 教學 003 函數
Ptyhon 教學 003 函數Ptyhon 教學 003 函數
Ptyhon 教學 003 函數
信宏 陳
 
0710 php學習進度(1)
0710 php學習進度(1)0710 php學習進度(1)
0710 php學習進度(1)
K- Peggy
 
Hi Haskell
Hi HaskellHi Haskell
Hi Haskell
Jifeng Deng
 

What's hot (20)

C++基礎程式設計 ch3 條件判斷
C++基礎程式設計 ch3 條件判斷C++基礎程式設計 ch3 條件判斷
C++基礎程式設計 ch3 條件判斷
 
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
 
看似比較簡單堆推坑教學 C語言崩潰到崩潰(一)
看似比較簡單堆推坑教學 C語言崩潰到崩潰(一)看似比較簡單堆推坑教學 C語言崩潰到崩潰(一)
看似比較簡單堆推坑教學 C語言崩潰到崩潰(一)
 
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)
看似比較簡單的推坑教學 C語言從崩潰到崩潰Ex(一)
 
C++基礎程式設計 ch2 變數與運算子
C++基礎程式設計 ch2 變數與運算子C++基礎程式設計 ch2 變數與運算子
C++基礎程式設計 ch2 變數與運算子
 
Java 開發者的函數式程式設計
Java 開發者的函數式程式設計Java 開發者的函數式程式設計
Java 開發者的函數式程式設計
 
Compiler for Dummy 一點都不深入的了解 Compiler, Interpreter 和 VM
Compiler for Dummy 一點都不深入的了解 Compiler, Interpreter 和 VMCompiler for Dummy 一點都不深入的了解 Compiler, Interpreter 和 VM
Compiler for Dummy 一點都不深入的了解 Compiler, Interpreter 和 VM
 
5, sed
5, sed5, sed
5, sed
 
Ihome inaction 篇外篇之fp介绍
Ihome inaction 篇外篇之fp介绍Ihome inaction 篇外篇之fp介绍
Ihome inaction 篇外篇之fp介绍
 
Javascript 培训第三节 基础下
Javascript 培训第三节 基础下Javascript 培训第三节 基础下
Javascript 培训第三节 基础下
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1
 
compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹compiler fuzzer : prog fuzz介紹
compiler fuzzer : prog fuzz介紹
 
C++基礎程式設計 ch5 陣列
C++基礎程式設計 ch5 陣列C++基礎程式設計 ch5 陣列
C++基礎程式設計 ch5 陣列
 
Wasm text
Wasm textWasm text
Wasm text
 
5, initialization & cleanup
5, initialization & cleanup5, initialization & cleanup
5, initialization & cleanup
 
改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法
 
Ptyhon 教學 003 函數
Ptyhon 教學 003 函數Ptyhon 教學 003 函數
Ptyhon 教學 003 函數
 
SCJP ch10
SCJP ch10SCJP ch10
SCJP ch10
 
0710 php學習進度(1)
0710 php學習進度(1)0710 php學習進度(1)
0710 php學習進度(1)
 
Hi Haskell
Hi HaskellHi Haskell
Hi Haskell
 

Viewers also liked

使用 Spark 計算 differential expression
使用 Spark 計算 differential expression使用 Spark 計算 differential expression
使用 Spark 計算 differential expression
Drake Huang
 
The Rails 4 Way Chapter 1
The Rails 4 Way Chapter 1The Rails 4 Way Chapter 1
The Rails 4 Way Chapter 1
Drake Huang
 
Rails 5 All topic Notes
Rails 5 All  topic NotesRails 5 All  topic Notes
Rails 5 All topic Notes
Manish Sakariya
 
Git Tutorial
Git TutorialGit Tutorial
Git Tutorial
Drake Huang
 
A Short Guide to the E-utilities
A Short Guide to the E-utilitiesA Short Guide to the E-utilities
A Short Guide to the E-utilities
Drake Huang
 
Introduction to MySQL and phpMyAdmin
Introduction to MySQL and phpMyAdminIntroduction to MySQL and phpMyAdmin
Introduction to MySQL and phpMyAdmin
Drake Huang
 
Software Testing Project: Testing csmap program
Software Testing Project: Testing csmap programSoftware Testing Project: Testing csmap program
Software Testing Project: Testing csmap program
Drake Huang
 
Real Time with Rails 5
Real Time with Rails 5Real Time with Rails 5
Real Time with Rails 5
Lucas Renan
 
Rails 5 subjective overview
Rails 5 subjective overviewRails 5 subjective overview
Rails 5 subjective overview
Jan Berdajs
 
Ruby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesRuby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 Features
PhraseApp
 

Viewers also liked (10)

使用 Spark 計算 differential expression
使用 Spark 計算 differential expression使用 Spark 計算 differential expression
使用 Spark 計算 differential expression
 
The Rails 4 Way Chapter 1
The Rails 4 Way Chapter 1The Rails 4 Way Chapter 1
The Rails 4 Way Chapter 1
 
Rails 5 All topic Notes
Rails 5 All  topic NotesRails 5 All  topic Notes
Rails 5 All topic Notes
 
Git Tutorial
Git TutorialGit Tutorial
Git Tutorial
 
A Short Guide to the E-utilities
A Short Guide to the E-utilitiesA Short Guide to the E-utilities
A Short Guide to the E-utilities
 
Introduction to MySQL and phpMyAdmin
Introduction to MySQL and phpMyAdminIntroduction to MySQL and phpMyAdmin
Introduction to MySQL and phpMyAdmin
 
Software Testing Project: Testing csmap program
Software Testing Project: Testing csmap programSoftware Testing Project: Testing csmap program
Software Testing Project: Testing csmap program
 
Real Time with Rails 5
Real Time with Rails 5Real Time with Rails 5
Real Time with Rails 5
 
Rails 5 subjective overview
Rails 5 subjective overviewRails 5 subjective overview
Rails 5 subjective overview
 
Ruby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesRuby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 Features
 

Similar to Ruby 使用手冊 (Part 1)

實踐大學教案20140329
實踐大學教案20140329實踐大學教案20140329
實踐大學教案20140329Mu-Fan Teng
 
RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試
Wen-Tien Chang
 
Learning notes ruby
Learning notes rubyLearning notes ruby
Learning notes rubyRoger Xia
 
Bash入门基础篇
Bash入门基础篇Bash入门基础篇
Bash入门基础篇Zhiyao Pan
 
02.python基础
02.python基础02.python基础
02.python基础
modou li
 
Ruby程式語言入門導覽
Ruby程式語言入門導覽Ruby程式語言入門導覽
Ruby程式語言入門導覽
Mu-Fan Teng
 
2, bash synax simplified
2, bash synax simplified2, bash synax simplified
2, bash synax simplified
ted-xu
 
Bash shell script 教學
Bash shell script 教學Bash shell script 教學
Bash shell script 教學
Ming-Sian Lin
 
JCConf 2023 - 深入淺出 Java 21 功能
JCConf 2023 - 深入淺出 Java 21 功能JCConf 2023 - 深入淺出 Java 21 功能
JCConf 2023 - 深入淺出 Java 21 功能
Joseph Kuo
 
介绍&第一章
介绍&第一章介绍&第一章
介绍&第一章
贺 利华
 
第9章 Shell 編程
第9章 Shell 編程第9章 Shell 編程
第9章 Shell 編程kidmany2001
 
6, awk
6, awk6, awk
6, awk
ted-xu
 
第1章 入门
第1章 入门第1章 入门
第1章 入门
Runa Jiang
 
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
彼得潘 Pan
 
Cypher 查询语言
Cypher 查询语言Cypher 查询语言
Cypher 查询语言
zernel
 
来自 Google 的 r 语言编码风格指南
来自 Google 的 r 语言编码风格指南来自 Google 的 r 语言编码风格指南
来自 Google 的 r 语言编码风格指南
学峰 司
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记
Lingfei Kong
 
RSpec & TDD Tutorial
RSpec & TDD TutorialRSpec & TDD Tutorial
RSpec & TDD Tutorial
Wen-Tien Chang
 
12, string
12, string12, string
12, string
ted-xu
 

Similar to Ruby 使用手冊 (Part 1) (20)

Ruby basic
Ruby basicRuby basic
Ruby basic
 
實踐大學教案20140329
實踐大學教案20140329實踐大學教案20140329
實踐大學教案20140329
 
RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試
 
Learning notes ruby
Learning notes rubyLearning notes ruby
Learning notes ruby
 
Bash入门基础篇
Bash入门基础篇Bash入门基础篇
Bash入门基础篇
 
02.python基础
02.python基础02.python基础
02.python基础
 
Ruby程式語言入門導覽
Ruby程式語言入門導覽Ruby程式語言入門導覽
Ruby程式語言入門導覽
 
2, bash synax simplified
2, bash synax simplified2, bash synax simplified
2, bash synax simplified
 
Bash shell script 教學
Bash shell script 教學Bash shell script 教學
Bash shell script 教學
 
JCConf 2023 - 深入淺出 Java 21 功能
JCConf 2023 - 深入淺出 Java 21 功能JCConf 2023 - 深入淺出 Java 21 功能
JCConf 2023 - 深入淺出 Java 21 功能
 
介绍&第一章
介绍&第一章介绍&第一章
介绍&第一章
 
第9章 Shell 編程
第9章 Shell 編程第9章 Shell 編程
第9章 Shell 編程
 
6, awk
6, awk6, awk
6, awk
 
第1章 入门
第1章 入门第1章 入门
第1章 入门
 
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
 
Cypher 查询语言
Cypher 查询语言Cypher 查询语言
Cypher 查询语言
 
来自 Google 的 r 语言编码风格指南
来自 Google 的 r 语言编码风格指南来自 Google 的 r 语言编码风格指南
来自 Google 的 r 语言编码风格指南
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记
 
RSpec & TDD Tutorial
RSpec & TDD TutorialRSpec & TDD Tutorial
RSpec & TDD Tutorial
 
12, string
12, string12, string
12, string
 

Ruby 使用手冊 (Part 1)

  • 1. Ruby 使用手冊 Part 1 Compass Study Group Jian-Long Huang 2016.05.03
  • 2. 甚麼是 Ruby? • 直譯式腳本語言 (interpreted scripting language):  能夠直接產生作業系統呼叫  強大的字串處理 (string operations) 及正規表示式 (regular expressions)  在開發時提供即時回饋 • 快捷易用:  不需要變數宣告 (variable declarations)  變數不需要型別 (typed)  語法簡單一致  自動管理記憶體 2
  • 3. 甚麼是 Ruby? • 物件導向程式設計:  任何東西都是物件  類別、方法、繼承等  單件方法 (singleton methods)  模組提供「混入」(mixin) 功能  迭代器 (iterators) 及閉包 (closures) • 以及:  多精確度整數 (multiple precision integers)  方便的例外處理 (exception processing)  動態載入 (dynamic loading)  執行緒支援 (threading support) 3
  • 4. 起步走 • 顯示 Ruby 版本 • 執行 Ruby 命令 • 執行 Ruby 檔 $ ruby –v ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux] $ ruby -e 'puts "hello world"' hello world $ echo "puts 'hello world'" > hello.rb $ ruby hello.rb hello world 4
  • 5. 安裝 Ruby • Windows  RubyInstaller for Windows • Linux (Ubuntu) • Linux (CentOS) • Mac OS X • 更多參考 https://www.ruby-lang.org/zh_tw/documentation/installation/ $ sudo apt-get install ruby $ sudo yum install ruby $ brew install ruby 5
  • 6. 簡單示範 • 計算階乘 # 計算出數字的階乘 # 請把程式儲存為 fact.rb def fact(n) if n == 0 1 else n * fact(n-1) end end puts fact(ARGV[0].to_i) 6
  • 7. 簡單示範 • ARGV: 命令列引數 • to_i: 將字元字串 (character string) 轉換為整數 • 不加 return,回傳值即為最後一個 statement 的傳回值。 7
  • 8. 互動模式 • 執行 ruby 而未輸入引數,Ruby 會從標準輸入 (standard input) 讀取指令,在輸 入結束後 (送出 Ctrl-D 指令) 開始執行。 • 輸入 irb 指令,可以進入互動模式。 8
  • 9. 字串 (String) • 使用雙引號或單引號括起 • 雙引號可以使用字串內插 (string interpolation) • 雙引號可以使用反斜線轉義字符 a = "foo" a = 'bar' a = "foo" b = "#{a}bar" # ==> "foobar" a = "foon" # ==> 輸出 foo + 換行符號 9
  • 10. 字串 (String) • 串接字串 • 重複字串 • 提取字串 a = "foo" + "bar" # ==> "foobar" a = "foo" * 2 # ==> "foofoo" a = "parsley" a[0] # ==> "p" a[-1] # ==> "y" a[0,1] # ==> "p" a[-2,2] # ==> "ey" a[0..3] # ==> "pars" a[0...3] # ==> "par" a[-5..-2] # ==> "rsle" 10
  • 11. 字串 (String) • 字串比對 "foo" == "foo" # ==> true "foo" == "bar" # ==> false 11
  • 12. 正規表示式 • 和 Perl 一樣,Ruby 使用 string =~ /<pattern>  發現符合,傳回字串中符合之位置  沒有符合,傳回 nil • 更多參考 http://ruby-doc.org/core-2.1.1/Regexp.html # 抓出手機號碼 phone = "123-456-7890" if phone =~ /(d{3})-(d{3})-(d{4})/ ext = $1 city = $2 num = $3 end 12
  • 13. 陣列 (Array) • 使用中括號 ([]) 建立陣列,元素可不同型態 • 陣列串接 • 陣列重複 • 使用 join 轉成字串 • 字串使用 split 轉成陣列 ary = [1, 2, "3"] ary + ["foo", "bar"] # ==> [1, 2, "3", "foo", "bar"] ary * 2 # ==> [1, 2, "3", 1, 2, "3"] ary.join(":") # ==> "1:2:3" "1:2:3".split(":") # ==> ["1", "2", "3"] 13
  • 14. 雜湊 (Hash) • 使用大括號 ({}) 建立雜湊,key 和 value 可為不同型態 h = {1 => 2, "2" => "4"} # ==> {1=>2, "2"=>"4"} # 使用 symbol 當作 key,有兩種寫法 h = {:foo => 1, :bar => 2} h = {foo: 1, bar: 2} 14
  • 15. 字串符號 (Symbol) • 一個 Symbol 物件皆為冒號開頭,例如 :foo 或 :foo_bar。它的作用就是代表一 個名字,最大的作用就是當做 Hash 的鍵 (key)。例如 { :foo => 1, :bar => 2 },在這裡我們只需要一個識別的名字,不需要使用字串物件。 • 使用 Symbol 寫法更簡潔和獲得效能上的好處。 • 更多參考 http://kaochenlong.com/2016/04/25/string-and-symbol/ 15
  • 16. Percent Literals (Percent Strings) • http://bearsu.logdown.com/posts/304719-ruby-percent-literals 16
  • 17. Ruby 是完全物件導向 • 在 Ruby,每樣東西都是物件,使用 if、while 等條件判斷時,空字串、空陣列、 空雜湊都視為 true。 17
  • 18. 繼續簡單示範 • 猜字程式 01 words = ['foobar', 'baz', 'quux'] 02 secret = words[rand(3)] 03 04 print "guess?" 05 while guess = STDIN.gets 06 guess.chop! 07 if guess == secret 08 puts "You win!" 09 break 10 else 11 puts "Sorry, you lose." 12 end 13 print "guess?" 14 end 15 puts "the word is "+ secret + " . " 18
  • 19. 繼續簡單示範 • rand(3): 隨機傳回 0, 1, 2 其中一個數值 • STDIN.gets 或 gets:收集使用者輸入的一行內容,直到使用者輸入 Ctrl-D • chop: 刪除字串的最後一個字元並回傳結果 • chop!: 同 chop,但會永久改變物件本身 19
  • 20. 繼續簡單示範 • 正規表示式 01 st = "033[7m" 02 en = "033[m" 03 04 puts "Enter an empty string at any time to exit." 05 06 while true 07 print "str> "; STDOUT.flush; str=gets.chop 08 break if str.empty? 09 print "pat> "; STDOUT.flush; pat=gets.chop 10 break if pat.empty? 11 re = Regexp.new(pat) 12 puts str.gsub(re, "#{st}&#{en}") 13 end 20
  • 21. 繼續簡單示範 • empty?: 在定義方法的時候名稱可以是任何符號,一般來說結尾加上 ? 表示只會 傳回 Boolean 值,是 Ruby 的慣例。 • gsub: globally substitute 的縮寫,將匹配的內容取代成另外一個字串。  &: 上個匹配的對象 (last match) 21
  • 22. 控制結構 • case i=8 case i when 1, 2..5 puts "1..5" when 6..10 puts "6..10" when 'aaa', 'bbb' puts "aaa or bbb" when /def/ # 正規表示式 puts "includes /def/" end 22
  • 23. 控制結構 • while i = 0 while i < 3 puts i += 1 end # 可以簡寫成 puts i += 1 while i < 3 23
  • 24. 控制結構 • if i = 0 if i == 0 puts "It's zero." end # 可以簡寫成 puts "It's zero" if i == 0 24
  • 25. 控制結構 • unless: if 的否定型 • until: while 的否定型 • 何時用 if,何時用 unless?  一般來說,儘量使用正向表達,少用 ! 符號 # Good puts "It's nil" if foo.nil? puts "It's not nil" unless foo.nil? # Not good puts "It's not nil" if !foo.nil? 25
  • 26. 控制結構 • 中斷迴圈  break: 中斷所有迴圈  next: 中斷目前迴圈,從下個迴圈開始  redo: 重新執行目前迴圈  return: 中斷迴圈並跳出迴圈所在的方法 • for for num in (4..6) puts num end for elt in [100, -9.6, "pickle"] puts elt end 26
  • 27. 控制結構 • for 的另一種寫法 # 如果你習慣用 C 或 Java,你可能會比較喜歡這個。 for element in collection ... end # Smalltalk 程式員則可能喜歡這個。 collection.each { |element| ... } collection.each do |element| ... end 27
  • 28. 迭代器 (Iterator) • 字串常用的迭代器  each_byte: 取出每個字元  each_line: 取出每一行 • 定義迭代器:  在 function 使用 yield ,該 function 就變成迭代器。  yield 可將控制移至傳遞往迭代器的程式碼區塊 (block of code)。 def repeat(num) while num > 0 yield num -= 1 end end repeat(3) { puts "foo" } 28