使用target.write:
尝试将5行减少为1时出现上述错误target.write("%s \n %s \n %s \n") % [line1, line2, line3]
# target.write("\n")
# target.write(line2)
# target.write("\n")
# target.write(line3)
# target.write("\n")
已评论的部分已运行(当第一行为target.write(line1)
时,但这不是。
为什么第一行与编写注释掉的六行不一样?这是整个剧本:
filename = ARGV.first
script = $0
puts "We're going to erase #{filename}."
puts "If you don't want that hit control C"
puts "If you do want that hit execute or 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 "line1: "; line1 = STDIN.gets.chomp()
print "line2: "; line2 = STDIN.gets.chomp()
print "line3: "; line3 = STDIN.gets.chomp()
puts "I'm going to write these to the file."
target.write("%s \n %s \n %s \n") % [line1, line2, line3]
# target.write("\n")
# target.write(line2)
# target.write("\n")
# target.write(line3)
# target.write("\n")
puts "And finally we close it"
target.close()
答案 0 :(得分:0)
target.write
行的关闭位置错误。尝试
target.write("%s \n %s \n %s \n" % [line1, line2, line3])
答案 1 :(得分:0)
你的括号错了。你有什么:
target.write("%s \n %s \n %s \n") % [line1, line2, line3]
等同于:
a = target.write("%s \n %s \n %s \n")
a % [line1, line2, line3]
因此你想要的是:
target.write("%s \n %s \n %s \n" % [line1, line2, line3])