Ruby 返回包含属性的字符串
我有这个ruby代码用于我的班级球员和我的班级游戏,我在网球俱乐部工作Ruby 返回包含属性的字符串,ruby,tdd,Ruby,Tdd,我有这个ruby代码用于我的班级球员和我的班级游戏,我在网球俱乐部工作 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.sco
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
我得到的错误是预期为“30个iguales”,但得到的是nil
。
我的所有其他测试都通过了,但这次测试失败了,我无法解决这个问题。Ruby方法将返回最后一个经过计算的表达式,即使没有显式的
return
语句,我想这就是你在这里要做的。问题是,对于测试数据,该方法将计算“#{@player_one.score}iguales”
,但随后继续计算第二个if
语句,并通过它返回nil
您要修复的选项有:
if
语句,而不是两个。或:请在开始时解释代码的用途。大多数读者不会理解“职业游戏”和“职业玩家”。
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