所以,我通過http://www.rubykoans.com/努力工作,我卡在about_scoring_project.rb。這是我刺傷得分的方法。爲什麼Ruby包含?評估爲零?
def score(dice)
score = 0;
if dice.respond_to?("include?") then
# add 1000 points if rolled 3 ones
score += 1000 if dice.include?([1, 1, 1])
# add 100 points times die face value if rolled 3 of a number between 2 and 6
(2...6).each do |die|
score += die*100 if dice.include?([die, die, die])
# award points for each 1 or 5 not a part of a set of 3
leftovers = dice - [1,1,1]
leftovers -= [5,5,5]
leftovers.each do |leftover|
score += 100 if leftover == 1
score += 50 if leftover == 5
end
end
end
score
end
class AboutScoringAssignment < EdgeCase::Koan
def test_score_examples
assert_equal 1150, score([1,1,1,5,1])
assert_equal 0, score([2,3,4,6,2])
assert_equal 350, score([3,4,5,3,3])
assert_equal 250, score([1,5,1,2,4])
end
end
在呼叫從第一assert_equal進球,我希望dice.include?([1,1,1]),以評估爲真,但它評估爲零(和得分返回0,而不是的1150)。
這個我試過單獨...
require 'test/unit'
class EnumerableTests < Test::Unit::TestCase
def test_include
my_enumerable = [1,1,1,5,1]
assert true, my_enumerable.include?([1,1,1])
end
end
...並測試通過,所以我不知道爲什麼我在我的得分方法越來越爲零。
有人看到我做錯了什麼?
好問題,但重複的[確定一個數組是否包含另一個數組在紅寶石](http://stackoverflow.com/questions/2855174/determining-whether-one-array-contains-the-contents-另一陣列在紅寶石) – 2010-09-17 00:14:09