<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                我們將做一些關於函式和變數的練習,以確認你真正掌握了這些知識。這節習題對你來說可以說是一本道(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 一次。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看