-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_guesser.rb
51 lines (40 loc) · 1013 Bytes
/
web_guesser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Jumpstart Lab WebGuesser Sinatra Project
# http://tutorials.jumpstartlab.com/projects/web_guesser.html
require 'sinatra'
require 'sinatra/reloader' if development?
set :secret_number, rand(100)
get "/" do
guess = params["guess"]
message = check_guess(guess)
erb :index, :locals => {:number => settings.secret_number, :message => message}
end
def check_guess(guess)
if guess.is_a? String
guess = guess.to_i
if guess - 5 > settings.secret_number
"Way Too High"
elsif guess > settings.secret_number
"Too high"
elsif guess + 5 < settings.secret_number
"Way too low"
elsif guess < settings.secret_number
"Too Low"
else
"You got it! The secret number is #{settings.secret_number}"
end
else
"Try and guess the secret number"
end
end
__END__
@@index
<html>
<head></head>
<body>
<p><%= message %><p>
<form>
<input type='text' name='guess' />
<input type='submit' value='Guess!' />
</form>
</body>
</html>