楼主: AlexQin

《 笨方法學 Ruby 》(Learn Ruby The Hard Way)

[复制链接]
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
31#
 楼主| 发表于 2013-11-18 13:48 | 只看该作者
習題 22: 到現在你學到了哪些東西?

這節以及下一節的習題中不會有任何代碼,所以也不會有習題答案或者加分習題。其實這節習題可以說是一個巨型的加分習題。我將讓你完成一個表格,讓你回顧你到現在學到的所有東西。

首先,回到你的每一個習題的腳本裡,把你碰到的每一個詞和每一個符號(symbol,character 的別名)寫下來。確保你的符號列表是完整的。

下一步,在每一個關鍵詞和符號後面寫出它的名字,並且說明它的作用。如果你在書裡找不到符號的名字,就上網找一下。如果你不知道某個關鍵字或者符號的作用,就回到用到該符號的章節通讀一下,並且在腳本中測試一下這個符號的用處。

你也許會碰到一些橫豎找不到答案的東西,只要把這些記在列表裡,它可以提示你讓你知道還有哪些東西不懂,等下次碰到的時候,你就不會輕易跳過了。

等你記住了這份列表中的所有內容,就試著把這份列表默寫一遍。如果發現自己漏掉或者無法從「記憶中」回想起某些內容,就回去再記一遍。

Warning: 做這節習題沒有失敗,只有嘗試,請牢記這一點。

你學到的東西

這種記憶練習是枯燥無味的,所以知道它的意義很重要。它會讓你明確目標,讓你知道你所有努力的目的。

在這節練習中你學會的是各種符號的名稱,這樣讀程式碼這件事對你來說會更加容易。這和學英語時記憶字母表和基本單詞的意義是一樣的,不同的是 Ruby 中會有一些你不熟悉的符號。

慢慢來,別讓它成為你的負擔。這些符號對你來說應該比較熟悉,所以記住它們應該不是很費力的事情。你可以一次花個15 分鐘,然後休息一下。適度讓你的大腦休息一下可以讓你學得更快,而且可以讓你保持士氣。

使用道具 举报

回复
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
32#
 楼主| 发表于 2013-11-18 13:49 | 只看该作者
習題 23: 閱讀一些程式碼

經過上一周的練習,你應該已​​經牢記了你的符號列表。現在你需要再花一周的時間,應用這些知識,在網上閱讀程式碼。這個任務初看會覺得很艱鉅。我將直接把你丟到深水區呆幾天,讓你竭盡全力去讀懂實實在在的專案裡的程式碼。這節練習的目的不是讓你讀懂,而是讓你學會下面的技能:

  • 找到你需要的 Ruby 程式碼。
  • 通讀程式碼,找到檔案。
  • 嘗試理解你找到的程式碼。
  • 以你現在的水準,你還不具備完全理解你找到的程式碼的能力,不過通過接觸這些程式碼,你可以熟悉真正的程式專案會是什麼樣子。


當你做這節習題時,你可以把自己當成是一個人類學家來到了一片陌生的大陸,你只懂得一丁點本地語言,但你需要接觸當地人並且生存下去。當然做習題不會碰到生存問題,這畢竟這不是荒野或者叢林。

你要做的事情如下:

  • 使用你的瀏覽器登錄 github.com,搜尋「ruby」。
  • 隨便找一個專案,然後點進去。
  • 點擊 Source 標籤,瀏覽目錄和檔案列表,直到你看到以 .rb 結尾的檔案
  • 從頭開始閱讀你找到的程式碼。把它的功能用筆記記下來。
  • 如果你看到一些有趣的符號或者奇怪的字串,你可以把它們記下來,日後再進行研究。

就是這樣,你的任務是使用你目前學到的東西,看自己能不能讀懂一些程式碼,看出它們的功能來。你可以先粗略地閱讀,然後再細讀。也許你還可以試試將難度比較大的部分一字不漏地朗讀出來。

現在再試試其它的幾個站:

  • heroku.com
  • rubygems.org
  • bitbucket.org


在這些網站你可能還會看到以 .c 結尾的奇怪文件,不過你只需要看 .rb 結尾的檔案就可以了。

最後一個有趣的事情是你可以在這四個​​網站搜索 「ruby」以外的你感興趣的話題,例如你可以搜索「journalism(新聞)」,「cooking(下廚)」,「physics(物理)」,或者任何你感興趣的話題。你也許會找到一些對你有用的,且可以直接拿來用的程式碼。


使用道具 举报

回复
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
33#
 楼主| 发表于 2013-11-18 13:50 | 只看该作者
習題 24: 更多練習

你離這本書第一部分的結尾已經不遠了,你應該已經具備了足夠的 Ruby 基礎知識,可以繼續學習一些程式的原理了,但你應該做更多的練習。這個練習的內容比較長,它的目的是鍛煉你的毅力,下一個習題也差不多是這樣的,好好完成它們,做到完全正確,記得仔細檢查。

  1. puts "Let's practice everything."
  2. puts "You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs."

  3. poem = <<MULTI_LINE_STRING

  4. \tThe lovely world
  5. with logic so firmly planted
  6. cannot discern \n the needs of love
  7. nor comprehend passion from intuition
  8. and requires an explanation
  9. \n\t\twhere there is none.

  10. MULTI_LINE_STRING

  11. puts "--------------"
  12. puts poem
  13. puts "--------------"

  14. five = 10 - 2 + 3 - 6
  15. puts "This should be five: #{five}"

  16. def secret_formula(started)
  17.   jelly_beans = started * 500
  18.   jars = jelly_beans / 1000
  19.   crates = jars / 100
  20.   return jelly_beans, jars, crates
  21. end

  22. start_point = 10000
  23. beans, jars, crates = secret_formula(start_point)

  24. puts "With a starting point of: #{start_point}"
  25. puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."

  26. start_point = start_point / 10

  27. puts "We can also do that this way:"
  28. puts "We'd have %s beans, %s jars, and %s crates." % secret_formula(start_point)
复制代码

你應該看到的結果

  1. $ ruby ex24.rb
  2. Let's practice everything.
  3. You'd need to know 'bout escapes with \ that do
  4. newlines and    tabs.
  5. --------------

  6.     The lovely world
  7. with logic so firmly planted
  8. cannot discern
  9. the needs of love
  10. nor comprehend passion from intuition
  11. and requires an explanation

  12.         where there is none.

  13. --------------
  14. This should be five: 5
  15. With a starting point of: 10000
  16. We'd have 5000000 beans, 5000 jars, and 50 crates.
  17. We can also do that this way:
  18. We'd have 500000 beans, 500 jars, and 5 crates.
  19. $
复制代码

加分習題

  • 記得仔細檢查結果,從後往前倒著檢查,把程式碼朗讀出來,在不清楚的位置加上註釋。
  • 故意將程式碼改爛,執行並檢查會發生什麼樣的錯誤,並且確認你有能力改正這些錯誤。

使用道具 举报

回复
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
34#
 楼主| 发表于 2013-11-19 16:45 | 只看该作者

習題 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. $
复制代码

我們來逐行分析一下每一步實現的是什麼:

  • 在第 2 行你 require 了自己的 ./ex25.rb Ruby 檔案,和你做過的其他 require 一樣$。在 require 的時候你不需要加 .rb 後綴。這個過程裡,你將這個檔案當做了一個 module (模組)來使用,你在這個模組裡定義的函式也可以直接呼叫出來。
  • 第 4 行你創造了一個用來處理的 sentence (句子)。
  • 第 6 行你使用了 Ex25 模組呼叫了你的第一個函式 Ex25.break_words。其中的 . (dot, period) 符號可以告訴 Ruby:「Hi,我要執行 Ex25 裡的那個叫 break_word 的函式!」
  • 第 8 行我們只是輸入 Ex25.sort_words 來得到一個排序過的句子。
  • 10-15 行我們使用 Ex25.print_first_word 和 Ex25.print_last_word 將第一個和最後一個詞印出來。
  • 第 16 行比較有趣。我把 words 變數寫錯成了 wrods,所以 Ruby 在 17-18 行給了一個錯誤訊息。
  • 第 19-20 行我們印出了修改過後的詞彙列表。第一個和最後一個詞我們已經印過了,所以在這裡沒有再印出來。
  • 剩下的行你需要自己分析一下,就留作你的加分習題了。

加分習題

  • 研究答案中沒有分析過的行,找出它們的來龍去脈。確認自己明白了自己使用的是模組 Ex25 中定義的函式。
  • 我們將我們的函式放在一個 module 裡式因為他們擁有自己的 命名空間 (namespace)。這樣如果有其他人寫了一個函式也叫 break_words,這樣就不會發生碰創。無論如何,輸入 Ex25. 是一件很煩人的事。有一個比較方便的作法,你可以輸入 include Ex25,這相當於說:「我要將所有 Ex25 這個 mudle 裡的所有東西 include 到我現在的 module 裡。」
  • 試著在你正在使用 IRB 時,弄爛檔案會發生什麼事。你可能要執行 CTRL-D ( Windows下是CTRL-Z ) 才能把 IRB 關掉 reload 一次。

使用道具 举报

回复
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
35#
 楼主| 发表于 2013-11-19 16:46 | 只看该作者
本帖最后由 AlexQin 于 2013-11-19 16:47 编辑

習題 26: 恭喜你,現在來考試了!

你已經差不多完成這本書的前半部分了,不過後半部分才是更有趣的。你將學到邏輯,並通過條件判斷實現有用的功能。

在你繼續學習之前,你有一道試題要做。這道試題很難,因為它需要你修正別人寫的程式碼。當你成為程式設計師以後,你將需要經常面對別的程式設計師的程式碼,也許還有他們的傲慢態度,他們會經常說自己的程式碼是完美的。

這樣的程式設計師是自以為是不在乎別人的蠢貨。優秀的科學家會對他們自己的工作持懷疑態度,同樣,優秀的程式設計師也會認為自己的程式碼總有出錯的可能,他們會先假設是自己的程式碼有問題,然後用排除法清查所有可能是自己有問題的地方,最後才會得出「這是別人的錯誤」這樣的結論。

在這節習題中,你將面對一個程度糟糕的程式設計師,並改好他的程式碼。我將習題 24 和25 胡亂拷貝到了一個檔案中,隨機地刪掉了一些字,然後添加了一些錯誤進去。大部分的錯誤是 Ruby 在執行時會告訴你的,還有一些算術錯誤是你要自己找出來的。再剩下來的就是格式和拼寫錯誤了

所有這些錯誤都是程式設計師很容易犯的,就算有經驗的程式設計師也不例外。

你的任務是將此檔案修改正確,用你所有的技能改進這個腳本。你可以先分析這個文件,或者你還可以把它像學期論文一樣印出來,修正裡邊的每一個缺陷,重複修正和運行的動作,直到這個腳本可以完美地運行起來。在整個過程中不要尋求幫助,如果你卡在某個地方無法進行下去,那就休息一會晚點再做。

就算你需要幾天才能完成,也不要放棄,直到完全改對為止。

最後要說的是,這個練習的目的不是寫程式,而是修正現有的程式,習題放在下面的網站:

http://ruby.learncodethehardway.org/book/exercise26.txt

從那裡把程式碼複製過來,命名為 ex26.rb,這也是本書唯一一處允許你複製貼上的地方。



  1. # This function will break up words for us.
  2. def break_words(stuff)
  3.     words = stuff.split(' ')
  4.     return words
  5. end

  6. # Sorts the words.
  7. def sort_words(words)
  8.     return sorted(words)
  9. end

  10. # Prints the first word after popping it off.
  11. def puts_first_word(words)
  12.     word = words.poop(0)
  13.     puts word
  14. end

  15. # Prints the last word after popping it off.
  16. def puts_last_word(words)
  17.     word = words.pop(-1
  18.     puts word
  19. end

  20. # Takes in a full sentence and returns the sorted words.
  21. def sort_sentence(sentence)
  22.     words = break_words(sentence)
  23.     return sort_words(words)
  24. end

  25. # Puts the first and last words of the sentence.
  26. def puts_first_and_last(sentence)
  27.     words = break_words(sentence)
  28.     puts_first_word(words)
  29.     puts_last_word(words)
  30. end

  31. # Sorts the words then prints the first and last one.
  32. def puts_first_and_last_sorted(sentence)
  33.     words = sort_sentence(sentence)
  34.     puts_first_word(words)
  35.     puts_last_word(words)
  36. end


  37. puts "Let's practice everything."
  38. puts 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

  39. poem = <<POEM
  40. \tThe lovely world
  41. with logic so firmly planted
  42. cannot discern \n the needs of love
  43. nor comprehend passion from intuition
  44. and requires an explantion
  45. \n\t\twhere there is none.
  46. POEM


  47. puts "--------------"
  48. puts poem
  49. puts "--------------"

  50. five = 10 - 2 + 3 - 5
  51. puts "This should be five: %s" % five

  52. def secret_formula(started)
  53.     jelly_beans = started * 500
  54.     jars = jelly_beans \ 1000
  55.     crates = jars / 100
  56.     return jelly_beans, jars, crates
  57. end

  58. start_point = 10000
  59. beans, jars, crates == secret_formula(start-point)

  60. puts "With a starting point of: %d" % start_point
  61. puts "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)

  62. start_point = start_point / 10

  63. puts "We can also do that this way:"
  64. puts "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont


  65. sentence = "All god\tthings come to those who weight."

  66. words = ex25.break_words(sentence)
  67. sorted_words = ex25.sort_words(words)

  68. puts_first_word(words)
  69. puts_last_word(words)
  70. .puts_first_word(sorted_words)
  71. puts_last_word(sorted_words)
  72. sorted_words = ex25.sort_sentence(sentence)
  73. prin sorted_words

  74. puts_irst_and_last(sentence)

  75. puts_first_a_last_sorted(senence)
复制代码


使用道具 举报

回复
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
36#
 楼主| 发表于 2013-11-19 16:48 | 只看该作者
習題 27: 記住邏輯關係

到此為止你已經學會了讀寫檔案,命令列處理,以及很多 Ruby 數學運算功能。

今天,你將要開始學習邏輯了。你要學習的不是研究院裡的高深邏輯理論,只是程式設計師每天都用到的讓程式跑起來的基礎邏輯知識。

學習邏輯之前你需要先記住一些東西。這個練習我要求你一個星期完成,不要擅自修改 schedule,就算你煩得不得了,也要堅持下去。這個練習會讓你背下來一系列的邏輯表格,這會讓你更容易地完成後面的習題。

需要事先警告你的是:這件事情一開始一點樂趣都沒有,你會一開始就覺得它很無聊乏味,但它的目的是教你程式設計師必須的一個重要技能 — 一些重要的概念是必須記住的,一旦你明白了這些概念,你會獲得相當的成就感,但是一開始你會覺得它們很難掌握,就跟和烏賊摔跤一樣,而等到某一天,你會刷的一下豁然開朗。你會從這些基礎的記憶學習中得到豐厚的回報。

這裡告訴你一個記住某樣東西,而不讓自己抓狂的方法:在一整天裡,每次記憶一小部分,把你最需要加強的部分標記起來。不要想著在兩小時內連續不停地背誦,這不會有什麼好的結果。不管你花多長時間,你的大腦也只會留住你在前15 或者30 分鐘內看過的東西。

取而代之,你需要做的是創建一些索引卡片,卡片有兩列內容,正面寫下邏輯關係,反面寫下答案。你需要做到的結果是:拿出一張卡片來,看到正面的表達式,例如「True or False」,你可以立即說出背面的結果是「True」!堅持練習,直到你能做到這一點為止。

一旦你能做到這一點了,接下來你需要每天晚上自己在筆記​​本上寫一份真值表出來。不要只是抄寫它們,試著默寫真值表,如果發現哪裡沒記住的話,就飛快地撇一眼這裡的答案。這樣將訓練你的大腦讓它記住整個真值表。

不要在這上面花超過一周的時間,因為你在後面的應用過程中還會繼續學習它們。

邏輯術語

在 Ruby 中我們會用到下面的術語(符號或者詞彙)來定義事物的真(True)或者假(False)。電腦的邏輯就是在程式的某個位置檢查這些符號或者變數組合在一起表達的結果是真是假。

  • and 和
  • or 或
  • not 非
  • != (not equal) 不等於
  • == (equal) 等於
  • >= (greater-than-equal) 大於等於
  • <= (less-than-equal) 小於等於
  • true 真
  • false 假


其實你已經見過這些符號了,但這些詞彙你可能還沒見過。這些詞彙(and, or, not)和你期望的效果其實是一樣的,跟英語裡的意思一模一樣。

真值表

我們將使用這些符號來創建你需要記住的真值表。

NOT        True?
not False        True
not True        False
OR        True?
True or False        True
True or True        True
False or True        True
False or False        False
AND        True?
True and False        False
True and True        True
False and True        False
False and False        False
NOT OR        True?
not (True or False)        False
not (True or True)        False
not (False or True)        False
not (False or False)        True
NOT AND        True?
not (True and False)        True
not (True and True)        False
not (False and True)        True
not (False and False)        True
!=        True?
1 != 0        True
1 != 1        False
0 != 1        True
0 != 0        False
==        True?
1 == 0        False
1 == 1        True
0 == 1        False
0 == 0        True
現在使用這些表格創建你自己的卡片,再花一個星期慢慢記住它們。記住一點,這本書不會要求你成功或者失敗,只要每天盡力去學,在盡力的基礎上多花一點功夫就可以了。

使用道具 举报

回复
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
37#
 楼主| 发表于 2013-11-19 16:51 | 只看该作者
習題 28: 布林(Boolean)表示式練習

上一節你學到的邏輯組合的正式名稱是「布林邏輯表示式(boolean logic expression)」。在程式中,布林邏輯可以說是無處不在。它們是電腦運算的基礎和重要組成部分,掌握它們就跟學音樂掌握音階一樣重要。

在這節練習中,你將在 IRB 裡使用到上節學到的邏輯表示式。先為下面的每一個邏輯問題寫出你認為的答案,每一題的答案要嘛為 True 要嘛為 False。寫完以後,你需要將 IRB 運行起來,把這些邏輯語句輸入進去,確認你寫的答案是否正確。

  1. 1. true and true
  2. 2. false and true
  3. 3. 1 == 1 and 2 == 1
  4. 4. "test" == "test"
  5. 5. 1 == 1 or 2 != 1
  6. 6. true and 1 == 1
  7. 7. false and 0 != 0
  8. 8. true or 1 == 1
  9. 9. "test" == "testing"
  10. 10. 1 != 0 and 2 == 1
  11. 11. "test" != "testing"
  12. 12. "test" == 1
  13. 13. not (true and false)
  14. 14. not (1 == 1 and 0 != 1)
  15. 15. not (10 == 1 or 1000 == 1000)
  16. 16. not (1 != 10 or 3 == 4)
  17. 17. not ("testing" == "testing" and "Zed" == "Cool Guy")
  18. 18. 1 == 1 and not ("testing" == 1 or 1 == 0)
  19. 19. "chunky" == "bacon" and not (3 == 4 or 3 == 3)
  20. 20. 3 == 3 and not ("testing" == "testing" or "Ruby" == "Fun")
复制代码

在本節結尾的地方我會給你一個理清複雜邏輯的技巧。

所有的布林邏輯式都可以用下面的簡單流程得到結果:

  • 找到相等判斷的部分 (== or !=),將其改寫為其最終值(True 或False)。
  • 找到括號裡的 and/or,先算出它們的值。
  • 找到每一個 not,算出他們反過來的值。
  • 找到剩下的 and/or,解出它們的值。
  • 等你都做完後,剩下的結果應該就是 True 或者 False 了。


下面我們以 #20 邏輯式示範一下:

3 != 4 and not ("testing" != "test" or "Ruby" == "Ruby")

接下來你將看到這個複雜表達式是如何逐級解析為一個單獨結果的:

1. 找出每一個等值判斷:
  • 3 != 4 為 True: true and not ("testing" != "test" or "Ruby" == "Ruby")
  • "testing" != "test" 為 True: true and not (true or "Ruby" == "Ruby")
  • "Ruby" == "Ruby": true and not (true or true)
2. 找到 () 中的每一個 and/or :
  • (true or true) is True: true and not (true)
3. 找到每一個not 並將其逆轉:
  • not (true) is False: true and false
4. 找到剩下的and/or,解出它們的值:
  • true and false is False

這樣我們就解出了它最終的值為 False .

Warning: 雜的邏輯表達式一開始看上去可能會讓你覺得很難。而且你也許已經碰壁過了,不過別灰心,這些「邏輯體操」式的訓練只是讓你逐漸習慣起來,這樣後面你可以輕易應對程式裡邊更酷的一些東西。只要你堅持下去,不放過自己做錯的地方就行了。如果你暫時不太能理解也沒關係,弄懂的時候總會到來的。

你應該看到的結果

以下內容是在你自己猜測結果以後,通過和 IRB 對話得到的結果:

  1. $ irb
  2. ruby-1.9.2-p180 :001 > true and true
  3. => true
  4. ruby-1.9.2-p180 :002 > 1 == 1 and 2 == 2
  5. => true
复制代码

加分習題

  • Ruby 裡還有很多和 !=、==類似的操作符號。試著盡可能多的列出 Ruby 中的「等價運算符號」。例如 < 或是 <=。
  • 寫出每一個等價運算符號的名稱。例如 != 叫「not equal(不等於」。
  • 在 IRB 裡測試新的布林邏輯式。在敲 Enter 前你需要喊出它的結果。不要思考,憑自己的第一直覺就可以了。把表達式和結果用筆寫下來再敲 Enter,最後看自己做對多少,做錯多少。
  • 把習題 3 那張紙丟掉,以後你不再需要查詢它了。

使用道具 举报

回复
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
38#
 楼主| 发表于 2013-11-19 16:53 | 只看该作者
習題 29: 如果(if)

這裡是你接下要寫的作業,這段介紹了 if-statement (if 語句)。把這段輸入進去,讓它能夠正確執行。然後我們看看你是否有收穫。

  1. people = 20
  2. cats = 30
  3. dogs = 15

  4. if people < cats
  5.   puts "Too many cats! The world is doomed!"
  6. end

  7. if people > cats
  8.   puts "Not many cats! The world is saved!"
  9. end

  10. if people < dogs
  11.   puts "The world is drooled on!"
  12. end

  13. if people > dogs
  14.   puts "The world is dry!"
  15. end

  16. dogs += 5

  17. if people >= dogs
  18.   puts "People are greater than or equal to dogs."
  19. end

  20. if people <= dogs
  21.   puts "People are less than or equal to dogs."
  22. end

  23. if people == dogs
  24.   puts "People are dogs."
  25. end
复制代码

你應該看到的結果

  1. $ ruby ex29.rb
  2. Too many cats! The world is doomed!
  3. The world is dry!
  4. People are greater than or equal to dogs.
  5. People are less than or equal to dogs.
  6. People are dogs.
  7. $
复制代码

加分習題

猜猜「if 語句」是什麼,它有什麼用處。在做下一道習題前,試著用自己的話回答下面的問題:

  • 你認為 if 對於它下一行的程式碼做了什麼?
  • 把習題 29 中的其它布林表示式放到「if 語句」中會不會也可以運行呢?試一下。
  • 如果把變數 people、cats 和 dogs 的初始值改掉,會發生什麼事情?

使用道具 举报

回复
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
39#
 楼主| 发表于 2013-11-19 16:59 | 只看该作者

習題 30: Else 和 If

前一習題中你寫了一些「if 語句 (if-statements)」,並且試圖猜出它們是什麼,以及實現的是什麼功能。在你繼續學習之前,我給你解釋一下上一節的加分習題的答案。上一節的加分習題你做過了吧,有沒有?

  • 你認為 if 對於它下一行的代碼做了什麼?if 語句為程式碼創建了一個所謂的「分支(branch)」,就跟 RPG遊戲中的情節分支一樣。if 語句告訴你的腳本:「如果這個布林表示式為真,就執行接下來的程式碼,否則就跳過這一段。」
  • 把習題29中的其它布林表示式放到 if 語句中會不會也可以執行呢?試一下。可以。而且不管多複雜都可以,雖然寫複雜的東西通常是一種不好的寫作風格。
  • 如果把變數 people、cats和 dogs 的初始值改掉,會發生什麼事情?因為你比較的對象是數字,如果你把這些數字改掉的話,某些位置的 if 語句會被演繹為 True,而它下面的程式區段將被運行。你可以試著修改這些數字,然後在頭腦裡假想一下那一段程式碼會被運行。

把我的答案和你的答案比較一下,確認自己真正懂得程式碼「區段(block)」的含義。這點對於你下一節的習題習很重要,因為你將會寫很多的if 語句。

把這一段寫下來,並讓它運行起來:

  1. people = 30
  2. cars = 40
  3. buses = 15

  4. if cars > people
  5.   puts "We should take the cars."
  6. elsif cars < people
  7.   puts "We should not take the cars."
  8. else
  9.   puts "We can't decide."
  10. end

  11. if buses > cars
  12.   puts "That's too many buses."
  13. elsif buses < cars
  14.   puts "Maybe we could take the buses."
  15. else
  16.   puts "We still can't decide."
  17. end

  18. if people > buses
  19.   puts "Alright, let's just take the buses."
  20. else
  21.   puts "Fine, let's stay home then."
  22. end
复制代码

你應該看到的結果

  1. $ ruby ex30.rb
  2. We should take the cars.
  3. Maybe we could take the buses.
  4. Alright, let's just take the buses.
  5. $
复制代码

加分習題

  • 猜想一下 elsif 和 else 的功能。
  • 將 cars、people和buses的數量改掉,然後追溯每一個if語句。看看最後會印出什麼來。
  • 試著寫一些複雜的布林表示式,例如 cars > people and buses < cars。 在每一行的上面寫註解,說明這一行的功用。

使用道具 举报

回复
论坛徽章:
1056
紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-10-15 14:45:48紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:48:45
40#
 楼主| 发表于 2013-11-20 09:20 | 只看该作者
習題 31: 做出決定

這本書的上半部分,你印出了一些東西,並且呼叫了函式,不過一切都是直線式進行的。你的腳本從最上面一行開始,一路運行到結束,但其中並沒有決定程式流向的分支點。現在你已經學會了 if、else和 elsif,你就可以開始建立包含條件判斷的腳本了。

上一個腳本中你寫了一系列的簡單提問測試。這節的腳本中,你将需要向使用者提問,依據使用者的答案來做出決定。把腳本寫下來,多多搗鼓一陣子,看看他的運作原理是什麼。

  1. def prompt
  2.   print "> "
  3. end

  4. puts "You enter a dark room with two doors.  Do you go through door #1 or door #2?"

  5. prompt; door = gets.chomp

  6. if door == "1"
  7.   puts "There's a giant bear here eating a cheese cake.  What do you do?"
  8.   puts "1. Take the cake."
  9.   puts "2. Scream at the bear."

  10.   prompt; bear = gets.chomp

  11.   if bear == "1"
  12.     puts "The bear eats your face off.  Good job!"
  13.   elsif bear == "2"
  14.     puts "The bear eats your legs off.  Good job!"
  15.   else
  16.     puts "Well, doing #{bear} is probably better.  Bear runs away."
  17.   end

  18. elsif door == "2"
  19.   puts "You stare into the endless abyss at Cthuhlu's retina."
  20.   puts "1. Blueberries."
  21.   puts "2. Yellow jacket clothespins."
  22.   puts "3. Understanding revolvers yelling melodies."

  23.   prompt; insanity = gets.chomp

  24.   if insanity == "1" or insanity == "2"
  25.     puts "Your body survives powered by a mind of jello.  Good job!"
  26.   else
  27.     puts "The insanity rots your eyes into a pool of muck.  Good job!"
  28.   end

  29. else
  30.   puts "You stumble around and fall on a knife and die.  Good job!"
  31. end
复制代码

這裡的重點是你可以在 if 語句中內部再放一個 if 語句。這是一個很強大的功能,可以用來建立「巢狀(nested)」的決定(decision)。

你需要理解 if 語句包含 if 語句的概念。做一下加分習題,這樣你會確信自己真正理解了它們。

你應該看到的結果

我在玩一個小冒險遊戲。我的水準不怎麼樣。

  1. $ ruby ex31.rb
  2. You enter a dark room with two doors.  Do you go through door #1 or door #2?
  3. > 1
  4. There's a giant bear here eating a cheese cake.  What do you do?
  5. 1. Take the cake.
  6. 2. Scream at the bear.
  7. > 2
  8. The bear eats your legs off.  Good job!

  9. $ ruby ex31.rb
  10. You enter a dark room with two doors.  Do you go through door #1 or door #2?
  11. > 1
  12. There's a giant bear here eating a cheese cake.  What do you do?
  13. 1. Take the cake.
  14. 2. Scream at the bear.
  15. > 1
  16. The bear eats your face off.  Good job!

  17. $ ruby ex31.rb
  18. You enter a dark room with two doors.  Do you go through door #1 or door #2?
  19. > 2
  20. You stare into the endless abyss at Cthuhlu's retina.
  21. 1. Blueberries.
  22. 2. Yellow jacket clothespins.
  23. 3. Understanding revolvers yelling melodies.
  24. > 1
  25. Your body survives powered by a mind of jello.  Good job!

  26. $ ruby ex31.rb
  27. You enter a dark room with two doors.  Do you go through door #1 or door #2?
  28. > 2
  29. You stare into the endless abyss at Cthuhlu's retina.
  30. 1. Blueberries.
  31. 2. Yellow jacket clothespins.
  32. 3. Understanding revolvers yelling melodies.
  33. > 3
  34. The insanity rots your eyes into a pool of muck.  Good job!

  35. $ ruby ex31.rb
  36. You enter a dark room with two doors.  Do you go through door #1 or door #2?
  37. > stuff
  38. You stumble around and fall on a knife and die.  Good job!

  39. $ ruby ex31.rb
  40. You enter a dark room with two doors.  Do you go through door #1 or door #2?
  41. > 1
  42. There's a giant bear here eating a cheese cake.  What do you do?
  43. 1. Take the cake.
  44. 2. Scream at the bear.
  45. > apples
  46. Well, doing apples is probably better.  Bear runs away.
复制代码

加分習題

為遊戲添加新的部分,改變玩家做決定的位置。盡自己能力擴充這個遊戲,不過別把遊戲弄得太詭異了。

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表