Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

491
Visualizações
Ruby YAML serialization and unserialization

I have a problem with Object-Oriented Project Hangman - serialization part. I saved my code in serialize method, but when I try to unserialize it, I have a problem with it. I see all components of classes in the YAML file and when I try to reach them with code, I can't do it. I don't know where the problem is. Here is my serialize method and deserialize method.

 def serialize
    file = File.open('game.yml', 'w') do |f|
      YAML.dump(self, f)
    end
    file.close
    exit
  end
def deserialize
 File.open('game.yml', 'r') do |f|
      p YAML.load(f)
    end
    # p file['word']
  end

If you want to see my all codes, here is my GitHub and Repl
Repl : https://replit.com/@Burakkepuc/Hangman#main.rb
GitHub : https://github.com/Burakkepuc/Hangman

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think it's actually working fine.

YAML.load returns an different instance of Game. You can call methods on that instance, but you don't have any access to the properties.

def deserialize
 File.open('game.yml', 'r') do |f|
      loaded_game =  YAML.load(f)
      puts loaded_game.length # fails with method not found
    end
    # p file['word']
  end

add a reader to you Game class and then the above will report the value of the length in the newly load instance of Game.

attr_reader :length

I am not sure what you want to do with the loaded game, but perhaps you simply want to call guess on it?

def deserialize
 File.open('game.yml', 'r') do |f|
      loaded_game =  YAML.load(f)
      loaded_game.guess 
    end
  end
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda