我有以下代码,它挂起来将用户输入写入文件。我的错误是什么?
file=File.open("Debsfile.txt", "r+") do |file|
file.puts "This is Deb's file. It's a great file. It has many characters, words and lines."
file.puts "This is another line of text in Deb's file."
end
puts "What is your favorite color?"
color = gets
color = color.chomp
color.write
file.close
File.open("Debsfile.txt").readlines.each do |line|
puts line
end
答案 0 :(得分:1)
答案 1 :(得分:0)
file=File.open("Debsfile.txt", "a+") do |file|
file.puts "This is Deb's file. It's a great file. It has many characters, words and lines."
file.puts "This is another line of text in Deb's file."
#asks user for favorite color in console
puts "What is your favorite color?"
#assigns color to variable "color"
color = gets
#color.chomp cuts of the new line character so that you just have the user input
color = color.chomp
#adds the color to the end of the file
file.puts("Your favorite color is #{color}.") #writing the color to the file
结束#end文件打开
File.open(" Debsfile.txt")。readlines.each do | line | 放线 端