无法将数据添加到文件中

时间:2014-10-03 17:07:14

标签: ruby

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

我很困惑为什么by block不运行。我想创建一个家族树数据库,但它不允许我将数据添加到我打开的文件

1 个答案:

答案 0 :(得分:1)

我认为您正在尝试做类似的事情:

#!/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