【问题标题】:Trying to serialize with JSON a save method in my Ruby Hangman game尝试使用 JSON 序列化我的 Ruby Hangman 游戏中的保存方法
【发布时间】:2018-01-31 12:06:42
【问题描述】:

我正在尝试让我的 save_game() 方法工作,没有任何内容写入 JSON 文件。这是我在一般情况下使用 JSON 和序列化的第一个任务。我什至不确定我哪里出错了。

这些是我的序列化方法:

  def to_json
    JSON.generate({array: @array, filestuff: @filestuff, random_word: @random_word, cipher: @cipher, random_word2: @random_word2, counter: @counter})
  end

  def load
    game_file = File.read("saved.json")
    data = JSON.parse(game_file)
    @cipher = data["cipher"]
    @random_word2 = data["random_word2"]
    @counter = data["counter"]
  end

  def save_game(string)
    game_file = File.new("saved.json","w") 
    game_file.write(string)
    game_file.close
  end

这是我的程序,在第 92 行我尝试调用我的 save_game 方法。

require 'json'
load 'display.rb'

class Hangman
  attr_accessor :name
  @name = name

  def initialize
    puts "What is your name?"
    @name = gets.chomp
    puts "
################################################
                   HANGMAN
################################################

               _________
              |        
              |       |
              |       O
              |      /|\\
              |       |
              |      / \\
              |
              -----------------
Welcome #{@name} to Hangman. The computer will generate
a 5-12 letter random word. You will try to guess
that word one letter at a time. Try to solve the
puzzle before time runs out! 


"

  end
end

class Gameplay
  attr_accessor :array, :filestuff, :random_word, :cipher, :random_word2, :counter
  def initialize
  @array = []
  @filestuff = File.foreach('5text.txt') do |x|
      chomped = x.chomp
      @array << chomped if (chomped.length >= 5 and chomped.length <= 12)
    end
  @random_word = @array.sample
  @cipher = @random_word.gsub(/[a-z]/, '*').split(//)
  @random_word2 = @random_word.split(//)
  @counter = 5

  def to_json
    JSON.generate({array: @array, filestuff: @filestuff, random_word: @random_word, cipher: @cipher, random_word2: @random_word2, counter: @counter})
  end

  def load
    game_file = File.read("saved.json")
    data = JSON.parse(game_file)
    @cipher = data["cipher"]
    @random_word2 = data["random_word2"]
    @counter = data["counter"]
  end

  def save_game(string)
    game_file = File.new("saved.json","w") 
    game_file.write(string)
    game_file.close
  end

  def choice(n)
    @random_word2.each_with_index do |i,index|
      if i == n
        @cipher[index] = i 
      end 
    end 
      if n == @random_word2.join.to_s
        puts "You win"
        puts "would you like to start another game? Y/N"
        new_game = gets.chomp
        if new_game == "Y"
          Hangman.new
          else exit 
        end
      end
      if @random_word2.include?(n) == false
        @counter -= 1
        display
        puts "#{@counter} guesses remaining."
        puts "To save press 1"
        save = gets.chomp
        if save == "1"

#Will not save 
          save_game($b.to_json)
        end
      end
      if @counter == 0
        puts "would you like to start another game? Y/N"
        new_game = gets.chomp
        if new_game == "Y"
          else exit 
        end
      end 
        puts @cipher.join 
  end

  @counter = 5
  while @counter > 0 
    choice(gets.chomp)
  end

 end 
end
Hangman.new
$b = Gameplay.new

【问题讨论】:

    标签: json ruby serialization


    【解决方案1】:

    您需要close 文件以确保您的输出实际写入磁盘(“刷新”)。您可以手动,拨打close

    def save_game(string)
      game_file = File.new("saved.json","w") 
      game_file.write(string)
      game_file.close
    end
    

    或者,您可以使用File.open,它接受一个块并在块结束时closes 文件:

    File.open("saved.json", "w") do |game_file|
      game_file.write(string)
    end
    

    由于写入磁盘是一个缓慢的操作,Ruby(以及我现在能想到的所有语言)将推迟实际写入文件,直到它在缓冲区中积累了一定数量的文本。一旦达到此限制,它将刷新缓冲区并将其中的所有内容写入磁盘。为了确保在尝试写入文件时实际写入了所有文本,您需要在文件上调用 close,作为关闭它的一部分,Ruby 将刷新其缓冲区中剩余的所有内容。

    还有其他方法可以确保刷新您的内容,但是当您刚刚开始了解这些内容时,只要确保在完成读取或写入文件时始终使用close 文件就足够了。

    【讨论】:

    • 我的文件中仍然没有保存任何内容,但已编辑为“打开”。我尝试了两个版本。
    • 如果你使用open,你应该给它一个块。在没有块的情况下使用open 与使用File.new 完全相同,因此需要手动关闭
    • 现在编辑。在我的电脑上尝试过,但仍然没有运气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多