我們將做一些關於函式和變數的練習,以確認你真正掌握了這些知識。這節習題對你來說可以說是一本道(in2逃跑了!!!):寫程序,逐行研究,弄懂它。
不過這節習題還是有些不同,你不需要執行它,取而代之,你需要將它導入到 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 一次。
- 笨方法更簡單
- 習題 00: 準備工作
- 習題 01: 第一個程序
- 習題 02: 注釋和#號
- 習題 03: 數字和數學計算
- 習題 04: 變量的命名
- 習題 05: 更多的變量和輸出
- 習題 06: 字符串和文字
- 習題 07: 更多輸出
- 習題 08: 輸出,輸出
- 習題 09: 輸出,輸出,輸出~
- 習題 10: 那是啥?
- 習題 11: 提問
- 習題 12: 模塊
- 習題 13: 參數,解包,參數
- 習題 14: 提示和傳遞
- 習題 15: 讀取文件
- 習題 16: 操作文件
- 習題 17: 更多的文件操作
- 習題 18: 命名,變量,代碼,函數
- 習題 19: 函數和變量
- 習題 20: 函數和文件
- 習題 21: 函數可以傳入信息
- 習題 22: 到現在你學到了什么?
- 習題 23: 閱讀一些代碼
- 習題 24: 更多練習
- 習題 25: 更多更多的練習
- 習題 26: 恭喜你,現在來考試了!
- 習題 27: 記住邏輯關系
- 習題 28: Boolean表達式練習
- 習題 29: 如果
- 習題 30: Else 和 If
- 習題 31: 做出判斷
- 習題 32: 循環和數組
- 習題 33: While
- 習題 34: 存取數組里的元素
- 習題 35: 分支和函數
- 習題 36: 設計和測試
- 習題 37: 重視各種符號