Quote:
My tests failed to produce false/true, and instead provided nil.
The reason is that your code do not cover all cases.
Try something like this:
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
def position_taken?(board, index)
if board[index] == [" "] || nil # not sure what this is supposed to be
puts false
elsif board[index] != [" "]
puts true
else
# put a warning message here and you will have surprise
end
end
end
The reason is that
board[index] == [" "] || nil
and
board[index] != [" "]
are not complementary conditions.
In case second condition should be complement of first, the code should be simply:
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
def position_taken?(board, index)
if board[index] == [" "] || nil
puts false
elsif board[index] != [" "]
else
puts true
end
end
end
Nota: I have never used Ruby, I just know common usages in other languages.