0
get '/blackjack/*' do
  if params[:splat] == "/hit" and defined? session[:bj_game]
    erb :blackjack
  elsif params[:splat] == "/fold" and defined? session[:bj_game]
    session[:bj_hum].fold = true
    erb :blackjack
  else
    if defined? session[:bj_game]
      new_session_check
      score_check
      erb :blackjack
    else
      session[:bj_game] = false
      session[:score] = 0
      new_session_check
      score_check
      erb :blackjack
    end
  end
end

def new_session_check
  if session[:bj_game] == false
    session[:bj_hum] = Blackjack.new
    session[:bj_com] = Blackjack.new
    session[:bj_game] = true
  end
end

def score_check
  if session[:bj_hum].game_loop == false
    if session[:bj_hum].score.to_i > 0
      session[:score] += session[:bj_hum].score.to_i
      check_save(session[:score])
    else
      session[:score] = 0
    end
    session[:bj_game] = false
    session[:bj_hum] = Blackjack.new
    session[:bj_com] = Blackjack.new
  end
end

Whenever it gets to score check, I get NoMethodError - Undefined method 'game_loop' for NilClass.

However, if I start from the main page:

get '/' do
  session[:bj_game] = false
  session[:score] = 0
  erb :home
end

And click on the link to /blackjack from the home erb, it will work as the new_session_check saw that the variable was false and then created new instances of the Blackjack class(which does have attr_accessor for game_loop).

Why does it not register in the get '/blackjack' version?

http://pastebin.com/6EFpp5gh - Here is a mockup version you can run to verify this. First go to localhost:4567/blackjack and you will get an internal service error. Restart the server then go to localhost:4567 first and then localhost:4567/blackjack afterward and you will see that it works.

1 Answer 1

0

You want a helper:

get 'somepath' do
  score_check
end

helpers do
  def score_check
    # now you can access session from within here
  end
end
2
  • Was hoping that would work but same thing. I go to localhost:4567/blackjack manually and get an Internal Server Error with the same thing, but if I click on <a href="/blackjack" title="Blackjack">Blackjack</a> from '/', it'll work. Commented Jun 13, 2013 at 19:59
  • 1
    I made this mockup version that has the same error: pastebin.com/6EFpp5gh run it, then go directly to localhost:4567/blackjack/ it will get an internal server error, then restart it and go to localhost:4567 and then to localhost:4567/blackjack/ it won't get that error. Commented Jun 13, 2013 at 20:59

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.