Ruby 无法将数据添加到文件中

Ruby 无法将数据添加到文件中,ruby,Ruby,我不明白为什么by块不运行。我想创建一个家谱数据库,但它不允许我将数据添加到我打开的文件中我想您正在尝试这样做: file_1 = File.open('Data_family.txt', 'r') user1 = go while user1 != "stop" print "whould you like to create: " user1 = gets.chomp print "what is your relation: " relation = get

我不明白为什么by块不运行。我想创建一个家谱数据库,但它不允许我将数据添加到我打开的文件中

我想您正在尝试这样做:

file_1 = File.open('Data_family.txt', 'r')
user1 = go
while user1 != "stop"
    print "whould you like to create: "
    user1 = gets.chomp
    print "what is your relation: "
    relation = gets.chomp
    file_1.syswrite "this is your " + relation
    file_1.syswrite "\n"
end
file_1.close

你的问题是什么?如果你也把收到的错误信息放进去,这会有帮助。我猜在第二行,user1=go应该在引号内:user1=go。为什么它不允许我进入循环?你不能将数据写入以只读模式打开的文件。r表示只读。
#!/usr/bin/eval ruby 

File.open('Data_family.txt', 'w') do |file_1|
  loop do
    print "Who would you like to create: "
    user1 = gets.chomp
    break if user1 == "stop"
    print "what is your relation: "
    relation = gets.chomp
    file_1.puts "this is your " + relation
  end
end #File autocloses at the end of the block