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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                現在讓我們再學習幾種文件操作。我們將編寫一個 Ruby 腳本,將一個文件中的內容拷貝到另一個文件中。這個腳本很短,不過它會讓你對于文件操作有更多的了解。 ~~~ from_file, to_file = ARGV script = $0 puts "Copying from #{from_file} to #{to_file}" # we could do these two on one line too, how? input = File.open(from_file) indata = input.read() puts "The input file is #{indata.length} bytes long" puts "Does the output file exist? #{File.exists? to_file}" puts "Ready, hit RETURN to continue, CTRL-C to abort." STDIN.gets output = File.open(to_file, 'w') output.write(indata) puts "Alright, all done." output.close() input.close() ~~~ 你應該注意到了我們使用了一個很好用的函數 `File.exists`?。運作原理是將文件名字當做一個參數傳入,如果文件存在的話,它會傳回 `true`,如果不存在的話就返回 `false`。之后在這本書中,我們將會使用這個函數做更多的事情。 # 你應該看到的結果 * * * * * 如同你前面所寫的腳本,運行該腳本需要兩個參數,一個是待拷貝的文件位置,一個是要拷貝至的文件位置。如果我們使用以前的 `test.txt` 我們將看到如下的結果: ~~~ $ ruby ex17.rb test.txt copied.txt Copying from test.txt to copied.txt The input file is 81 bytes long Does the output file exist? False Ready, hit RETURN to continue, CTRL-C to abort. Alright, all done. $ cat copied.txt To all the people out there. I say I don't like my hair. I need to shave it off. $ ~~~ 該命令對于任何文件應該都是有效的。試試操作一些別的文件看看結果。不過當心別把你的重要文件給弄壞了。 > Warning: 你看到我用 cat 這個指令了吧?它只能在 Linux 和 OX 下使用,Windows 用戶可以使用 type 做到相同效果。 # 加分習題 * * * * * 1. 再多讀讀和 `require` 相關的資料,然后將 IRB 跑起來,試試 require 一些東西看能不能摸出門到。當然,即使搞不清楚也沒關系。 2. 這個腳本實在有點煩人。沒必要再拷貝之前都問一遍吧,沒必要在屏幕上輸出那么多東西。試著刪掉腳本的一些功能,使它用起來更友好。 3. 看看你能把這個腳本改到多短,我可以把它寫成一行。 4. 我使用了一個叫 `cat` 的東西,這個古老的命令用處是將兩個文件「連接(con_cat_enate)」在一起,不過實際上它最大的用處是輸出文件內容到屏幕是。你可以通過 `man cat` 命令了解到更多資訊。 5. 使用 Windows 的人,你們可以給自己找一個 cat 的替代品。關于 man 的東西就別想太多了。Windows 下沒這個指令。 6. 找出為什么需要在代碼中寫` output.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>

                              哎呀哎呀视频在线观看