我刚刚开始我的 ruby 之旅,而且对编程也很陌生。这是家庭作业的代码。我收到标题中描述的语法错误。我知道我在预期文件结尾时添加了一些额外的结尾?正确的?但是这个额外的“结束”在哪里。
编码:
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
def rps_result(move1,move2)
if (move1 == "r" && move2 == "s") || (move1 == "s" && move2 == p) || (move1 == "p" && move2 == "r")
return true
else
return false
end
结尾
def rps_game_winner(player1, player2)
strategy = /[rps]/
raise NoSuchStrategyError unless (move1 =~ strategy) & (move2 =~ strategy)
move1 = player1[1].downcase
move2 = player2[2].downcase
rps_result(move1, move2)? player1 : player2
end
end
def rps_tournament_winner(game)
round_winners = []
if game.length < 2
raise WrongNumberOfPlayersError
else
game.each_slice(2) do
|l1 , l2|
round_winners << rps_game_winner(l1, l2)
end
rps_tournament_winner(round_winners)
end
end
rps_tournament_winner([[[["Richard", "S"], ["Dave", "S"]], [["Richard", "R"], ["Michael", "S"]]], [[["Allen", "S"], ["Omer", "P"]], [["David E.", "R"], ["Richard X.", "P"]]]])