我正在构建一个应用程序,它采用标准输入来保存用户及其偏好。我应该将标准输入写入文本文件并将用户输入保存在那里吗?
命令行.rb
class CommandLine
def initialize(filename)
@file = File.open(filename, 'w')
end
def add_user(input)
@file = File.open('new_accounts.txt', 'r+')
@file.write(input)
puts input
end
def run
puts "Welcome to the Command Line Client!"
command = ''
while command != 'quit'
printf "enter command: "
input = gets.chomp
parts = input.split
command = parts[0]
case command
when 'quit' then puts 'Goodbye!'
when '-a' then add_user(parts[1..-1].join(" "))
else
puts 'Invalid command #{command}, please try again.'
end
end
end
end
a = CommandLine.new('new_accounts.txt')
a.run
假设我希望用户在命令行中输入“ -a tommy like apples ”,我希望它输出:
tommy likes apples
同一个用户 tommy 也可以输入“ -a tommy likes oranges ”,然后更新他之前的偏好:
tommy likes oranges
任何帮助/方向表示赞赏,谢谢!