1
我正在加入一些用Sinatra製作的hang子手應用程序的Ruby代碼。具體來說,我正在查看下面的post "/check" do
路徑/函數。的函數的第一個的兩個步驟是可預測的,即,它顯示了final_word
變量,然後char_clicked
變量,但隨後的調試器顯示此Hash.newRuby:什麼是Hash.new在這裏做什麼
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
在功能在移動之前。我不明白爲什麼post "/check"
正在創建一個新的散列。它在代碼中沒有明確說明,而且我不明白爲什麼會在幕後發生。我包括以下爲好correct_guess方法,後者在``
final_word = session[:word]
(rdb:3) n
hangman.rb:79
char_clicked = params[:char_clicked]
(rdb:3) n
/Users/mm/.rvm/gems/[email protected]/gems/sinatra-1.3.3/lib/sinatra/base.rb:916
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
(rdb:3) n
hangman.rb:80
correct_guess = Game.correct_guess?(char_clicked, final_word)
支票路徑調用
post "/check" do
debugger
final_word = session[:word]
char_clicked = params[:char_clicked]
correct_guess = Game.correct_guess?(char_clicked, final_word)
if correct_guess
session[:revealed_word] = Word.reveal(session[:revealed_word], char_clicked, final_word)
session[:chars_left] = Word.chars_left(session[:revealed_word])
else
session[:incorrect_guesses] += 1
end
win = Game.win?(session[:chars_left], session[:incorrect_guesses])
{:word => session[:revealed_word], :correct_guess => correct_guess, :incorrect_guesses => session[:incorrect_guesses], :win => win}.to_json
end
遊戲correct_guess方法
def correct_guess?(char_clicked, final_word)
final_word.include?(char_clicked)
end
這是怎麼回事左右'西納特拉-1.3.3/lib目錄/西納特拉/ base.rb'的916線? –
我認爲第925行是你正在談論的,創造冷漠的params哈希https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb – BrainLikeADullPencil
'params'是一種方法,也許它第一次調用'params'方法時,創建了一個緩存的「無差別訪問哈希」。 –