2017-10-15 222 views
-2

我有我的班級玩家和我的班級遊戲的紅寶石代碼,我正在打網球卡塔。返回帶有屬性的字符串

class Player 
    def score 
    if @player_one.points <3 and @player_two.points<3 
     "#{@player_one.score} #{@player_two.score}" 
    elsif @player_one.points == @player_two.points 
     "#{@player_one.score} iguales" 
    end 

    if @player_one.points == 3 and @player_two.points == 3 
     'deuce' 
    elsif (@player_one.points == @player_two.points + 1) and (@player_two.points >=3) 
     "Ventaja #{@player_one.name}" 
    elsif (@player_one.points >= 4) and (@player_one.points >= (@player_two.points + 2)) 
     "Game #{@player_one.name}" 
    end 
    end 
end 

而且這個測試不能通過,所有已經通過的其他測試都不在這裏。

it 'return 30-iguales when both players have 2 points scored' do 
    player_one = Player.new 
    player_two = Player.new 

    game = TennisGame.new(player_one, player_two) 
    player_one.wins_point 
    player_one.wins_point 

    player_two.wins_point 
    player_two.wins_point 
    expect(game.score).to eq("30 iguales") 
    end 

我得到的錯誤是「30 iguales」,但得到了nil。 我的所有其他測試都已通過,但在此失敗,我無法解決問題。

+0

請在開始時解釋一下你的代碼打算做什麼。大多數讀者不會理解「階級遊戲」和「階級玩家」。 –

回答

0

即使沒有明確的return語句,Ruby方法也會返回最後一個評估表達式,我認爲這就是您要做的。問題是,對於測試數據,該方法將評估"#{@player_one.score} iguales",但隨後繼續評估第二個if聲明,並一路通過它返回nil

您的選項來解決是:

  1. 使整個事情的單一if聲明,而不是兩個。或者:
  2. 顯式返回每個可能的字符串的值。
相關問題