Sinatra + Ruby: Random Number keeps changing every time I guess. Scope issue? -


i using sinatra build webguesser jumpstart labs. enter number text field in browser. click submit , supposed response saying if number low or high (or within 5). use shotgun load server. want able guess number without having random number change every time guess.

code:

require 'sinatra' require 'sinatra/reloader'   def check_guess(guess) if params["guess"].to_i == guess         "you got right!" elsif params["guess"].to_i > guess      if params["guess"].to_i > (guess + 5)         "way high!"     else          "close.. high!"     end elsif params["guess"].to_i < guess     if params["guess"].to_i < (guess - 5)         "way low!"     else         "close.. low!"     end end end    # home route '/'      secret_number = rand(100)     message = check_guess(secret_number)     erb :index, :locals => { :message => message } end 

currently, new random number every time guess doesn't help. feel may have secret_number scope-wise. thoughts?

every time there request "/", relevant code executed, generates (with warnings) new secret_number.

one way deal route different urls first guess (in case secret number should generated), , consecutive guesses (in case new secret number should not generated).

also, bad practice use constant changes on time.


Comments