title: Setting variable flash[:message] in ruby
body: <p>I have an <code>.erb</code> file that displays the following lines:</p>
  
  <pre><code>&lt;% if flash[:message]%&gt;
  &lt;p&gt;&lt;span class"error"&gt;&lt;%= flash[:message] %&gt;&lt;/p&gt;
  &lt;%end%&gt;
  </code></pre>
  
  <p>I would like to set the variable <code>flash[:message]</code> in a <code>.rb</code> file and am not sure how to do so. For whatever reason symbols in Ruby tend to confuse me a bit. They seem like pointers, but somewhat different.</p>
  
  <p><b>File 1:</b></p>
  
  <pre><code>class HangpersonGame
   attr_accessor :word
   attr_accessor :guesses
   attr_accessor :wrong_guesses 
  
    # add the necessary class methods, attributes, etc. here
    # to make the tests in spec/hangperson_game_spec.rb pass.
    # Get a word from remote "random word" service
    # def initialize()
    # end
  
    def initialize(word)
      @word = word 
      @guesses = "" 
      @wrong_guesses = ""
    end
  
    def guess(letter)
      # make sure the letter is actually a letter
      if letter == nil || !(letter.class == String &amp;&amp; letter =~ /^[A-z]$/i)
        raise ArgumentError
      end
  
      # handle different cases
      letter.downcase!
  
      # handle repeated guesses
      if @guesses.include?(letter) || @wrong_guesses.include?(letter)
        flash[message] = "You have already used that letter"
        return false
      end
  
      #handle case where guess is not a letter
      return false if letter.length != 1
  
      # finally handle check and return true
      if @word.include? letter
        @guesses &lt;&lt; letter
      else
        @wrong_guesses &lt;&lt; letter
      end
      return true
    end
  
    def word_with_guesses
      result = ''
      @word.split('').each do |char|
        if @guesses.include? char
          result &lt;&lt; char
        else
          result &lt;&lt; '-'
        end
      end
  
      return result
    end
  
    def check_win_or_lose
      if word_with_guesses.downcase == @word.downcase
        return :win
      elsif @wrong_guesses.length &gt;= 7
        return :lose
      else
        return :play
      end
    end
  
    # You can test it by running $ bundle exec irb -I. -r app.rb
    # And then in the irb: irb(main):001:0&gt; HangpersonGame.get_random_word
    #  =&gt; "cooking"   &lt;-- some random word
    def self.get_random_word
      require 'uri'
      require 'net/http'
      uri = URI('http://watchout4snakes.com/wo4snakes/Random/RandomWord')
      Net::HTTP.new('watchout4snakes.com').start { |http|
        return http.post(uri, "").body
      }
    end
  
  end
  </code></pre>
  
  <p><b>File 2:</b></p>
  
  <pre><code>&lt;h2&gt;Guess a letter&lt;/h2&gt;
  
  &lt;% if flash[:message] %&gt;
    &lt;p&gt;
      &lt;span class="error"&gt;&lt;%= flash[:message] %&gt;&lt;/span&gt;
    &lt;/p&gt;
  &lt;% end %&gt;
  
  &lt;p&gt;
    Wrong Guesses:
    &lt;span class="guesses"&gt;&lt;%= @game.wrong_guesses %&gt;&lt;/span&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
    Word so far:
    &lt;span class="word"&gt;&lt;%= @game.word_with_guesses %&gt;&lt;/span&gt;
  &lt;/p&gt;
  
  &lt;form action="/guess" method="post"&gt;
    &lt;input type="text" size="1" name="guess" autocomplete="off"/&gt;
    &lt;input type="submit" value="Guess!"/&gt;
  &lt;/form&gt;
  
  &lt;%= erb :new %&gt;
  </code></pre>
  
  <p><b>File 3:</b></p>
  
  <pre><code>require 'sinatra/base'
  require 'sinatra/flash'
  require './lib/hangperson_game.rb'
  
  class HangpersonApp &lt; Sinatra::Base
  
    enable :sessions
    register Sinatra::Flash
  
    before do
      @game = session[:game] || HangpersonGame.new('')
    end
  
    after do
      session[:game] = @game 
    end
  
    # These two routes are good examples of Sinatra syntax
    # to help you with the rest of the assignment
    get '/' do
      redirect '/new'
    end
  
    get '/new' do
      erb :new
    end
  
    post '/create' do
      # NOTE: don't change next line - it's needed by autograder!
      word = params[:word] || HangpersonGame.get_random_word
      # NOTE: don't change previous line - it's needed by autograder!
  
      @game = HangpersonGame.new(word)
      redirect '/show'
    end
  
    # Use existing methods in HangpersonGame to process a guess.
    # If a guess is repeated, set flash[:message] to "You have already used that letter."
    # If a guess is invalid, set flash[:message] to "Invalid guess."
    post '/guess' do
      letter = params[:guess].to_s[0]
      ### YOUR CODE HERE ###
  
      @game.guess(letter);
  
      redirect '/show'
    end
  
    # Everytime a guess is made, we should eventually end up at this route.
    # Use existing methods in HangpersonGame to check if player has
    # won, lost, or neither, and take the appropriate action.
    # Notice that the show.erb template expects to use the instance variables
    # wrong_guesses and word_with_guesses from @game.
    get '/show' do
      ### YOUR CODE HERE ###
      erb :show # You may change/remove this line
    end
  
    get '/win' do
      ### YOUR CODE HERE ###
      erb :win # You may change/remove this line
    end
  
    get '/lose' do
      ### YOUR CODE HERE ###
      erb :lose # You may change/remove this line
    end
  
  end
  </code></pre>