<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                你已經學過了 `STDIN.gets` 和 `ARGV`,這些是你開始學習讀取文件的必備基礎。你可能需要多多實驗才能明白它的運作原理,所以你要細心練習,并且仔細檢查結果。處理檔案需要非常仔細,如果不仔細的話,你可能會把有用的文件弄壞或者刪除。導致前功盡棄。 這節練習涉及到寫兩個檔案。一個正常的` ex15.rb` 文件,另外一個是 `ex15_sample.txt`,第二個文件并不是腳本,而是供你的腳本讀取的文件。以下是后者的內容: ~~~ This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. ~~~ 我們要做的是把該文件用我們的腳本「打開(open)」,然后輸出。然而把文件名 `ex15_sample.txt`「寫死(Hard Coding」在代碼不是一個好主意,這些信息應該是使用者輸入的才對。如果我們碰到其他文件要處理,寫死的文件名就會給你帶來麻煩了。解決方案是使用 `ARGV` 和 `STDIN.gets` 來從使用者端獲取信息,從而知道哪些文件該被處理。 ~~~ filename = ARGV.first prompt = "> " txt = File.open(filename) puts "Here's your file: #{filename}" puts txt.read() puts "Type the filename again:" print prompt file_again = STDIN.gets.chomp() txt_again = File.open(file_again) puts txt_again.read() ~~~ 這個腳本中有一些新奇的玩意,我們來快速地過一遍: 代碼的 1-3 行使用 `ARGV `來獲取文件名,這個你已經熟悉了。接下來第 4 行我們使用一個新的命令 `File.open`。現在請在命令窗口執行` ri File.open` 來讀讀它的說明。注意到這像你的腳本,它接收一個參數,并且傳回一個值,你可以將這個值賦予一個變量。這就是你打開文件的過程。 第 6 行我們輸出了一小行,但在第 7 行我們看到了新奇的東西。我們在 `txt` 上呼叫了一個函數。你從 `open` 獲得的東西是一個 `file` (文件),文件本身也支持一些命令。它接受命令的方式是使用句點 `.` (dot or period),緊跟著你的命令,然后參數。就像 `File.open` 做的事一樣。差別是:當你說 `txt.read()` 時,你的意思其實是:「嘿 txt!執行你的 read 命令,無需任何參數!」 腳本剩下的部份基本差不多,不過我就把剩下的分析作為加分習題留給你了。 # 你應該看到的結果 * * * * * 我的腳本叫 “ex15_sample.txt”,以下是執行結果: ~~~ $ ruby ex15.rb ex15_sample.txt Here's your file 'ex15_sample.txt': This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. I'll also ask you to type it again: > ex15_sample.txt This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. $ ~~~ # 加分習題 * * * * * 這節的難度跨越有點大,所以你要盡量做好這節加分習題,然后再繼續后面的章節。 1. 在每一行的上面用注釋說明這一行的用途。 2. 如果你不確定答案,就問別人,或者是上網查找。大部分時候,只要查找「ruby 你要查的東西」,就能得到你要的答案。比如查一下「ruby file.open」。 3. 我使用了「命令」這個詞,不過實際上他們的名字是「函數(function)」和「方法(method)」。上網查一下這兩者的意義和區別。看不懂也沒關系,迷失在其他程序設計師的知識海洋里是很正常的一件事。 4. 刪掉 9-15 行使用到` STDIN.gets `的部份,再執行一次腳本。 5. 只用 `STDIN.gets` 編寫這個腳本,想想哪種得到文件名的方法更好,以及為什么。 6. 執行 `ri File` 然后往下捲動直到看見 `read()` 命令(函數/方法)。看到很多其他的命令了吧。你可以玩其他試試。 7. 再次啟動 `IRB`,然后在里面使用` File.open `打開一個文件,這種 open 和 read 的方法也值得一學。 8. 讓你的腳本針對 `txt` 和 `txt_again` 變量執行一下 `close()`,處理完文件后你需要將其關閉,這是很重要的一點。
                  <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>

                              哎呀哎呀视频在线观看