So, I'm a total nuby working through http://www.rubykoans.com/ and I'm stuck on about_scoring_project.rb. This is my stab at the score method.
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
In the call to score from the first assert_equal, I would expect dice.include?([1,1,1]) to evaluate to true, but it's evaluating to nil (and score is returning 0 instead of 1150).
I tried this separately...
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
...and the test passes, so I don't know why I'm getting nil in my score method.
Anybody see what I'm doing wrong?