我用 Ruby 制作了一个简单的游戏来帮助学习它。现在,我一直在尝试在 Sinatra 中实现它,但是我无法让文本输入与“while”循环交互。谁能帮我看看我做错了什么?
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require 'haml'
#use Rack::Session::Pool
module HotColdApp
def initialize
guesses = 1
i = rand(10)
end
def play
"Guess a number from 1 to 10"
"You have 5 tries"
"----------"
guess = gets.to_i
while guess != i and guesses < 5
guesses = guesses + 1
if guess < i
"too cold"
guess = gets.to_i
else guess > i
"too hot"
guess = gets.to_i
end
end
if guess == i
"just right"
else
"try again next time"
end
end
end
include HotColdApp
get '/' do
p initialize
haml :index
end
post '/' do
guess = params[:guess]
haml :index, :locals => {:name => guess}
end
__END__
@@ index
!!!!
%html
%head
%title Hot/Cold
%body
%h1 hOt/cOld
%p
Guess a number from 1 to 10. You have 5 tries.
%form{:action => "/", :method => "POST"}
%p
%input{:type => "textbox", :name => "guess", :class => "text"}
%p
%input{:type => "submit", :value => "GUESS!", :class => "button"}
%p