如果你做了上一個練習的加分習題,你應該已經知道了各種文件相關的命令(方法/函數)。你應該記住的命令如下:
* close – 關閉文件。跟你編輯器的 `文件->儲存`filename = ARGV.first
script = $0
puts "We're going to erase #{filename}."
puts "If you don't want that, hit CTRL-C (^C)."
puts "If you do want that, hit RETURN."
print "? "
STDIN.gets
puts "Opening the file..."
target = File.open(filename, 'w')
puts "Truncating the file. Goodbye!"
target.truncate(target.size)
puts "Now I'm going to ask you for three lines."
print "line 1: "; line1 = STDIN.gets.chomp()
print "line 2: "; line2 = STDIN.gets.chomp()
print "line 3: "; line3 = STDIN.gets.chomp()
puts "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
puts "And finally, we close it."
target.close().. 是一樣的意思。
* read – 讀取文件內容。你可以把結果賦給一個變量。
* readline – 讀取文件文字中的一行。
* truncate – 清空文件,請小心使用該命令。
* write(stuff) – 將 stuff 寫入文件
這是你現在應該知道的重要命令。有些命令需要接收參數,但這對我們并不重要。你只要記住 write 的用法就可以了。 write 需要接收一個字串作為參數,從而將該字符串寫入文件。
讓我們來使用這些命令做一個簡單的文字編輯器吧:
~~~
filename = ARGV.first
script = $0
puts "We're going to erase #{filename}."
puts "If you don't want that, hit CTRL-C (^C)."
puts "If you do want that, hit RETURN."
print "? "
STDIN.gets
puts "Opening the file..."
target = File.open(filename, 'w')
puts "Truncating the file. Goodbye!"
target.truncate(target.size)
puts "Now I'm going to ask you for three lines."
print "line 1: "; line1 = STDIN.gets.chomp()
print "line 2: "; line2 = STDIN.gets.chomp()
print "line 3: "; line3 = STDIN.gets.chomp()
puts "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
puts "And finally, we close it."
target.close()
~~~
這是一個大文件,大概是你鍵入過的最大的文件。所以慢慢來,仔細檢查,讓它能夠跑起來。有一個小技巧就是你可以讓你的腳本一部分一部分地跑起來。先寫 1-8 行,讓它能跑起來,再多做 5 行,再接著幾行,以此類推,直到整個腳本都可以跑起來為止。
# 你應該看到的結果
* * * * *
你將看到兩樣東西,一樣是你新腳本的輸出:
~~~
$ ruby ex16.rb test.txt
We're going to erase 'test.txt'.
If you don't want that, hit CTRL-C (^C).
If you do want that, hit RETURN.
?
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines.
line 1: To all the people out there.
line 2: I say I don't like my hair.
line 3: I need to shave it off.
I'm going to write these to the file.
And finally, we close it.
$
~~~
這是一個大文件,大概是你鍵入過的最大的文件。所以慢慢來,仔細檢查,讓它能夠跑起來。有一個小技巧就是你可以讓你的腳本一部分一部分地跑起來。先寫 1-8 行,讓它能跑起來,再多做 5 行,再接著幾行,以此類推,直到整個腳本都可以跑起來為止。
# 加分習題
* * * * *
1. 如果你覺得自己沒有弄懂的話,用我們的老方法,在每一行之前加上注釋,為自己理清思路。就算不能理清思路,你也可以知道自己究竟具體哪里沒弄清楚。
2. 寫一個和上一個習題類似的腳本,使用` read `和` ARGV` 讀取你剛才新建立的文件。
3. 文件中重復的地方太多了。試著用一個 `target.write()` 將 `line1` , `line2 `, `line3` 輸出,你可以使用字符串、格式化字串以及轉義序列。
4. 找出為什么我們打開檔案時要使用 w 模式,而你真的需要 `target.truncate()` 嗎?去看 Ruby 的 `File.open `函數找答案吧。
- 笨方法更簡單
- 習題 00: 準備工作
- 習題 01: 第一個程序
- 習題 02: 注釋和#號
- 習題 03: 數字和數學計算
- 習題 04: 變量的命名
- 習題 05: 更多的變量和輸出
- 習題 06: 字符串和文字
- 習題 07: 更多輸出
- 習題 08: 輸出,輸出
- 習題 09: 輸出,輸出,輸出~
- 習題 10: 那是啥?
- 習題 11: 提問
- 習題 12: 模塊
- 習題 13: 參數,解包,參數
- 習題 14: 提示和傳遞
- 習題 15: 讀取文件
- 習題 16: 操作文件
- 習題 17: 更多的文件操作
- 習題 18: 命名,變量,代碼,函數
- 習題 19: 函數和變量
- 習題 20: 函數和文件
- 習題 21: 函數可以傳入信息
- 習題 22: 到現在你學到了什么?
- 習題 23: 閱讀一些代碼
- 習題 24: 更多練習
- 習題 25: 更多更多的練習
- 習題 26: 恭喜你,現在來考試了!
- 習題 27: 記住邏輯關系
- 習題 28: Boolean表達式練習
- 習題 29: 如果
- 習題 30: Else 和 If
- 習題 31: 做出判斷
- 習題 32: 循環和數組
- 習題 33: While
- 習題 34: 存取數組里的元素
- 習題 35: 分支和函數
- 習題 36: 設計和測試
- 習題 37: 重視各種符號