http://lrthw.github.io/ex16/ 根据这个教程的第 16 章敲的代码:确保和教程一样, 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) #这个就是 15 行
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()
有个奇怪的问题: 输出如下: C:\ruby\site>ruby test.rb bbb.txt We're going to erase bbb.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! test.rb:15: undefined method `size' for #File:bbb.txt (NoMethodError)
报错说第 15 行没有没 size 定义 method,请问是我电脑的环境问题吗?求解? 另外问下,代码的第二行,定义那个的作用是什么?