如果你做了上一个练习的加分习题,你应该已经了解了个个种文件相关的命令(方法/函式)。你应该记住的命令如下:
文件->儲存..
是一样的意思。这是你现在应该知道的重要命令。有些命令需要接收参数,但这对我们并不重要。你只要记住 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 行,再接着几行,以此类推,直到整个脚本都可以跑起来为止。
read
和 ARGV
读取你刚才新建立的文件。target.write()
将 line1
, line2
, line3
印出来,你可以使用字串、格式化字串以及跳脱字串。w
模式,而你真的需要 target.truncate()
吗?去看 Ruby 的 File.open
函式找答案吧。