0

I am new to ruby and I am trying to create a hangman game, to do so I need to create a new game each time the user click on a button.

First step is to create the same object each time the methode create will be called (it will be different because of the .sample.

I am trying to create an object with the initialize method in my model.rbfile.

Here is my code :

class Game < ApplicationRecord

  has_many :guesses

  def initialize(*)
    super
    @word_to_guess = word_to_guess
    @health_bar = 5
    @game_status = game_status
  end

  def word_to_guess
    words = [
      "spokesperson", "firefighter", "headquarters", "confession", "difficulty", "attachment", "mechanical",
      "accumulation", "hypothesis", "systematic", "attraction", "distribute", "dependence", "environment",
      "jurisdiction", "demonstrator", "constitution", "constraint", "consumption", "presidency", "incredible",
      "miscarriage", "foundation", "photography", "constituency", "experienced", "background", "obligation",
      "diplomatic", "discrimination", "entertainment", "grandmother", "girlfriend", "conversation", "convulsion",
      "constellation", "leadership", "insistence", "projection", "transparent", "researcher", "reasonable","continental",
      "excavation", "opposition", "interactive", "pedestrian", "announcement", "charismatic", "strikebreaker",
      "resolution", "professional", "commemorate", "disability", "collection", "cooperation", "embarrassment",
      "contradiction", "unpleasant", "retirement", "conscience", "satisfaction", "acquaintance", "expression",
      "difference", "unfortunate", "accountant", "information", "fastidious", "conglomerate", "shareholder",
      "accessible", "advertising", "battlefield", "laboratory", "manufacturer", "acquisition", "operational",
      "expenditure", "fashionable", "allocation", "complication", "censorship", "population", "withdrawal",
      "sensitivity", "exaggerate", "transmission", "philosophy", "memorandum", "superintendent", "responsibility",
      "extraterrestrial", "hypothesize", "ghostwriter", "representative", "rehabilitation", "disappointment",
      "understanding", "supplementary", "preoccupation"
          ]
    words.sample
  end

  def game_status
    game_status = ["Game not started yet", "In Game", "You win", "You loose"]
    game_status[0]
  end

end

The problem is that when I do a Game.new in console, it returns :

[1] pry(main)> Game.new
=> #<Game:0x00007fa79b613290 id: nil, word_to_guess: nil, health_bar: nil, created_at: nil, updated_at: nil, guesse_id: nil, game_status: nil>

So the instance is not created and I don't know why.

1 Answer 1

0

As you see, instance was created

But I don't recommend to use instance variables such way. Instance variables are stored in memory

Also it's not good idea to override "getters" for rails attributes such way

It's better to store such data in database as rails attributes

You can do like this

after_initialize do
  self.word_to_guess ||= generate_word_to_guess
  self.health_bar ||= 5
  self.game_status ||= "Game not started yet"
end

Also it looks that for health_bar and game_status is better to define default values in database

You can read more about callbacks:

https://guides.rubyonrails.org/active_record_callbacks.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.