Ruby の学習に役立つ簡単なゲームを 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