Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 返回包含属性的字符串_Ruby_Tdd - Fatal编程技术网

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

我有这个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.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