這裏是我試圖通過一個測試:紅寶石設置默認哈希值
def test_it_scores_a_double_word_score
play = Play.new(:word => "hello")
assert_equal 16, play.score(:word_multiplier => :double)
end
這裏是我的類:
class Play < ActiveRecord::Base
before_save { self.word = word.downcase }
validates :word, presence: true, length: { maximum: 7 }
def letter_scores
{"A"=>1, "B"=>3, "C"=>3, "D"=>2, "E"=>1, "F"=>4, "G"=>2, "H"=>4, "I"=>1, "J"=>8,
"K"=>5, "L"=>1, "M"=>3, "N"=>1, "O"=>1, "P"=>3, "Q"=>10, "R"=>1, "S"=>1, "T"=>1,
"U"=>1, "V"=>4, "W"=>4, "X"=>8, "Y"=>4, "Z"=>10}
end
def score(word_multiplier: :single)
word_multiplier = {:single => 1, :double => 2, :triple => 3}
word.upcase.chars.inject(0){|sum, letter| sum + letter_scores[letter]} * word_multiplier
end
end
所以我的想法是,我需要的默認值散列爲:單個。如果關鍵字/值對:word_multiplier =>:double被傳遞,那麼我需要散列返回2.
我有語法問題: 1)我只使用=符號在方法中設置默認值,不是哈希。如何做到這一點?
2)我把word_multiplier::放在括號中的單個,似乎在設置默認值時工作。但是:word_multiplier =>:single不起作用。到底是怎麼回事?