我們將做一些關於函式和變數的練習,以確認你真正掌握了這些知識。這節習題對你來說可以說是一本道:寫程式,逐行研究,弄懂它。
不過這節習題還是有些不同,你不需要執行它,取而代之,你需要將它導入到 Ruby 通過自己執行函式的方式運行。
~~~
module Ex25
def self.break_words(stuff)
# This function will break up words for us.
words = stuff.split(' ')
words
end
def self.sort_words(words)
# Sorts the words.
words.sort()
end
def self.print_first_word(words)
# Prints the first word and shifts the others down by one.
word = words.shift()
puts word
end
def self.print_last_word(words)
# Prints the last word after popping it off the end.
word = words.pop()
puts word
end
def self.sort_sentence(sentence)
# Takes in a full sentence and returns the sorted words.
words = break_words(sentence)
sort_words(words)
end
def self.print_first_and_last(sentence)
# Prints the first and last words of the sentence.
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
end
def self.print_first_and_last_sorted(sentence)
# Sorts the words then prints the first and last one.
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
end
end
~~~
首先以正常的方式?`ruby ex25.rb`?運行,找出裡面的錯誤,並把它們都改正過來。然後你需要接著下面的答案章節完成這節習題。
## 你應該看到的結果
這節練習我們將在你之前用來做算術的 Ruby 編譯器(IRB)裡,用交互的方式和你的`.rb`?作交流。
這是我做出來的樣子:
~~~
$ irb
irb(main):001:0> require './ex25'
=> true
irb(main):002:0> sentence = "All good things come to those who wait."
=> "All good things come to those who wait."
irb(main):003:0> words = Ex25.break_words(sentence)
=> ["All", "good", "things", "come", "to", "those", "who", "wait."]
irb(main):004:0> sorted_words = Ex25.sort_words(words)
=> ["All", "come", "good", "things", "those", "to", "wait.", "who"]
irb(main):005:0> Ex25.print_first_word(words)
All
=> nil
irb(main):006:0> Ex25.print_last_word(words)
wait.
=> nil
irb(main):007:0> Ex25.wrods
NoMethodError: undefined method `wrods' for Ex25:Module
from (irb):6
irb(main):008:0> words
=> ["good", "things", "come", "to", "those", "who"]
irb(main):009:0> Ex25.print_first_word(sorted_words)
All
=> nil
irb(main):010:0> Ex25.print_last_word(sorted_words)
who
=> nil
irb(main):011:0> sorted_words
=> ["come", "good", "things", "those", "to", "wait."]
irb(main):012:0> Ex25.sort_sentence(sentence)
=> ["All", "come", "good", "things", "those", "to", "wait.", "who"]
irb(main):013:0> Ex25.print_first_and_last(sentence)
All
wait.
=> nil
irb(main):014:0> Ex25.print_first_and_last_sorted(sentence)
All
who
=> nil
irb(main):015:0> ^D
$
~~~
我們來逐行分析一下每一步實現的是什麼:
1. 在第 2 行你 require 了自己的?`./ex25.rb`?Ruby 檔案,和你做過的其他 require 一樣$。在 require 的時候你不需要加?`.rb`?後綴。這個過程裡,你將這個檔案當做了一個?`module`?(模組)來使用,你在這個模組裡定義的函式也可以直接呼叫出來。
2. 第 4 行你創造了一個用來處理的?`sentence`?(句子)。
3. 第 6 行你使用了?`Ex25`?模組呼叫了你的第一個函式?`Ex25.break_words`。其中的?`.`?(dot, period) 符號可以告訴 Ruby:「Hi,我要執行?`Ex25`?裡的那個叫?`break_word`?的函式!」
4. 第 8 行我們只是輸入?`Ex25.sort_words`?來得到一個排序過的句子。
5. 10-15 行我們使用?`Ex25.print_first_word`?和?`Ex25.print_last_word`?將第一個和最後一個詞印出來。
6. 第 16 行比較有趣。我把?`words`?變數寫錯成了?`wrods`,所以 Ruby 在 17-18 行給了一個錯誤訊息。
7. 第 19-20 行我們印出了修改過後的詞彙列表。第一個和最後一個詞我們已經印過了,所以在這裡沒有再印出來。
8. 剩下的行你需要自己分析一下,就留作你的加分習題了。
## 加分習題
1. 研究答案中沒有分析過的行,找出它們的來龍去脈。確認自己明白了自己使用的是模組 Ex25 中定義的函式。
2. 我們將我們的函式放在一個?`module`?裡式因為他們擁有自己的 命名空間 (namespace)。這樣如果有其他人寫了一個函式也叫?`break_words`,這樣就不會發生碰創。無論如何,輸入?`Ex25.`?是一件很煩人的事。有一個比較方便的作法,你可以輸入?`include Ex25`,這相當於說:「我要將所有 Ex25 這個 mudle 裡的所有東西 include 到我現在的 module 裡。」
3. 試著在你正在使用 IRB 時,弄爛檔案會發生什麼事。你可能要執行 CTRL-D ( Windows下是CTRL-Z ) 才能把 IRB 關掉 reload 一次。
- 笨方法更簡單
- 習題 0: 準備工作
- 習題 1: 第一個程式
- 習題 2: 注釋和井號
- 習題 3: 數字和數學計算
- 習題 4: 變數(variable)和命名
- 習題 5: 更多的變數和印出
- 習題 6: 字串(string)和文字
- 習題 7: 更多印出
- 習題 8: 印出,印出
- 習題 9: 印出,印出,印出
- 習題 10: 那是什么?
- 習題 11: 提問
- 習題 12: 模組 (Module)
- 習題 13: 參數、解包、參數
- 習題 14: 提示和傳遞
- 習題 15: 讀取檔案
- 習題 16: 讀寫檔案
- 習題 17: 更多的檔案操作
- 習題 18: 命名、變數、程式碼、函式
- 習題 19: 函式和變數
- 習題 20: 函式和檔案
- 習題 21: 函式可以傳回東西
- 習題 22: 到現在你學到了哪些東西?
- 習題 23: 閱讀一些程式碼
- 習題 24: 更多練習
- 習題 25: 更多更多的練習
- 習題 26: 恭喜你,現在來考試了!
- 習題 27: 記住邏輯關系
- 習題 28: 布林(Boolean)表示式練習
- 習題 29: 如果(if)
- 習題 30: Else 和 If
- 習題 31: 做出決定
- 習題 32: 回圈和陣列
- 習題 33: While 回圈
- 習題 34: 存取陣列里的元素
- 習題 35: 分支 (Branches) 和函式 (Functions)
- 習題 36: 設計和測試
- 習題 37: 復習各種符號
- 習題 38: 閱讀程式碼
- 習題 39: 陣列的操作
- 習題 40: Hash, 可愛的 Hash
- 習題 41: 來自 Percal 25 號行星的哥頓人(Gothons)
- 習題 42: 物以類聚
- 習題 43: 你來制作一個游戲
- 習題 44: 評估你的游戲
- 習題 45: 物件、類和從屬關系
- 習題 46: 一個專案骨架
- 習題 47: 自動化測試
- 習題 48: 更進階的使用者輸入
- 習題 49: 創造句子
- 習題 50: 你的第一個網站
- 習題 51: 從瀏覽器中取得輸入
- 習題 52: 創造你的網頁游戲
- 下一步
- 一個老程式設計師的建議