0

我现在使用 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
4

1 回答 1

0

我建议您考虑使用YAML数据序列化程序。它创建了一个格式良好且可读的文件,其他语言可以轻松读取。

require 'pp'
require 'yaml'

hs = [["RANK", "PLAYER", "SCORE", "DATE"],
      ["1st", "-", 0, "-"],
      ["2nd", "-", 0, "-"],
      ["3rd", "-", 0, "-"],
      ["4th", "-", 0, "-"],
      ["5th", "-", 0, "-"],
      ["6th", "-", 0, "-"],
      ["7th", "-", 0, "-"],
      ["8th", "-", 0, "-"],
      ["9th", "-", 0, "-"],
      ["10th", "-", 0, "-"]]

puts hs.to_yaml

看起来像:

---
- - RANK
  - PLAYER
  - SCORE
  - DATE
- - 1st
  - '-'
  - 0
  - '-'
- - 2nd
  - '-'
  - 0
  - '-'
- - 3rd
  - '-'
  - 0
  - '-'
- - 4th
  - '-'
  - 0
  - '-'
- - 5th
  - '-'
  - 0
  - '-'
- - 6th
  - '-'
  - 0
  - '-'
- - 7th
  - '-'
  - 0
  - '-'
- - 8th
  - '-'
  - 0
  - '-'
- - 9th
  - '-'
  - 0
  - '-'
- - 10th
  - '-'
  - 0
  - '-'

保存到文件很容易:

File.write('path/to/file.yaml', hs.to_yaml)

重新阅读它也很容易:

hs = YAML.load_file('path/to/file.yaml')

阅读“ Yaml Cookbook ”,了解更多关于 Ruby 和 YAML 结构之间关系的信息。

也就是说,我会为您的高分表推荐不同的数据结构。使用散列的散列来代替难以访问的数组数组:

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: '' },
}

这将使 YAML 文件更易于理解:

---
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: ''

并将简化您对代码中表格的访问。

通常,您的代码需要进行重大清理。编写代码的一个非常重要的原则是“干”又名“不要重复自己”。您的代码中有大量冗余,可以大大简化。这需要在一个单独的问题中处理,但是,基本上你的分数处理代码可以简化为一个case语句和一个在哈希中存储值的方法。

于 2013-07-02T15:04:56.420 回答