加载中...

习题 25: 更多更多的练习


我们将做一些关于函式和变量的练习,以确认你真正掌握了这些知识。这节习题对你来说可以说是一本道:写程式,逐行研究,弄懂它。

不过这节习题还是有些不同,你不需要执行它,取而代之,你需要将它导入到 Ruby 通过自己执行函式的方式运行。

  1. module Ex25
  2. def self.break_words(stuff)
  3. # This function will break up words for us.
  4. words = stuff.split(' ')
  5. words
  6. end
  7. def self.sort_words(words)
  8. # Sorts the words.
  9. words.sort()
  10. end
  11. def self.print_first_word(words)
  12. # Prints the first word and shifts the others down by one.
  13. word = words.shift()
  14. puts word
  15. end
  16. def self.print_last_word(words)
  17. # Prints the last word after popping it off the end.
  18. word = words.pop()
  19. puts word
  20. end
  21. def self.sort_sentence(sentence)
  22. # Takes in a full sentence and returns the sorted words.
  23. words = break_words(sentence)
  24. sort_words(words)
  25. end
  26. def self.print_first_and_last(sentence)
  27. # Prints the first and last words of the sentence.
  28. words = break_words(sentence)
  29. print_first_word(words)
  30. print_last_word(words)
  31. end
  32. def self.print_first_and_last_sorted(sentence)
  33. # Sorts the words then prints the first and last one.
  34. words = sort_sentence(sentence)
  35. print_first_word(words)
  36. print_last_word(words)
  37. end
  38. end

首先以正常的方式 ruby ex25.rb 运行,找出里面的错误,并把它们都改正过来。然后你需要接着下面的答案章节完成这节习题。

你应该看到的结果

这节练习我们将在你之前用来做算术的 Ruby 编译器(IRB)里,用交互的方式和你的.rb 作交流。

这是我做出来的样子:

  1. $ irb
  2. irb(main):001:0> require './ex25'
  3. => true
  4. irb(main):002:0> sentence = "All good things come to those who wait."
  5. => "All good things come to those who wait."
  6. irb(main):003:0> words = Ex25.break_words(sentence)
  7. => ["All", "good", "things", "come", "to", "those", "who", "wait."]
  8. irb(main):004:0> sorted_words = Ex25.sort_words(words)
  9. => ["All", "come", "good", "things", "those", "to", "wait.", "who"]
  10. irb(main):005:0> Ex25.print_first_word(words)
  11. All
  12. => nil
  13. irb(main):006:0> Ex25.print_last_word(words)
  14. wait.
  15. => nil
  16. irb(main):007:0> Ex25.wrods
  17. NoMethodError: undefined method `wrods' for Ex25:Module
  18. from (irb):6
  19. irb(main):008:0> words
  20. => ["good", "things", "come", "to", "those", "who"]
  21. irb(main):009:0> Ex25.print_first_word(sorted_words)
  22. All
  23. => nil
  24. irb(main):010:0> Ex25.print_last_word(sorted_words)
  25. who
  26. => nil
  27. irb(main):011:0> sorted_words
  28. => ["come", "good", "things", "those", "to", "wait."]
  29. irb(main):012:0> Ex25.sort_sentence(sentence)
  30. => ["All", "come", "good", "things", "those", "to", "wait.", "who"]
  31. irb(main):013:0> Ex25.print_first_and_last(sentence)
  32. All
  33. wait.
  34. => nil
  35. irb(main):014:0> Ex25.print_first_and_last_sorted(sentence)
  36. All
  37. who
  38. => nil
  39. irb(main):015:0> ^D
  40. $

我们来逐行分析一下每一步实现的是什么:

  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 一次。

还没有评论.