我现在使用 YAML 来存储数据,它适用于创建和存储文件。我仍在尝试弄清楚如何使用 text-table gem 让表格以正确的格式打印到终端。这是代码:
def highscore
if File.exists?('highscore.txt')
hs = YAML.load_file("highscore.txt")
else
hs = {
1 => { player: '', score: 0, date: '' },
2 => { player: '', score: 0, date: '' },
3 => { player: '', score: 0, date: '' },
4 => { player: '', score: 0, date: '' },
5 => { player: '', score: 0, date: '' },
6 => { player: '', score: 0, date: '' },
7 => { player: '', score: 0, date: '' },
8 => { player: '', score: 0, date: '' },
9 => { player: '', score: 0, date: '' },
10 => { player: '', score: 0, date: '' },
}
end
(1..10).each do |rank|
t = Time.now
if @grand_total > hs[rank][:score]
hs[rank][:score] = @grand_total
hs[rank][:date] = "#{t.month}/#{t.day}/#{t.year}"
puts "Congratulations you set a new HIGH SCORE! Enter your initials."
initials = gets.chomp.upcase
hs[rank][:player] = initials
break
else
puts "Sorry, you didn't get a high score. Try again!"
end
end
File.write('highscore.txt', hs.to_yaml)
puts hs.to_table
end