Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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 单元测试错误1:Fixnum的未定义方法“rank”_Ruby_Unit Testing_Error Handling - Fatal编程技术网

Ruby 单元测试错误1:Fixnum的未定义方法“rank”

Ruby 单元测试错误1:Fixnum的未定义方法“rank”,ruby,unit-testing,error-handling,Ruby,Unit Testing,Error Handling,我的代码有问题,似乎无法确定需要更改什么。这是我的三个文件,底部是我得到的错误。我有18次完全相同的代码,其中一半给了我这个错误 初始化等级、套装和符号 def initialize(the_rank, the_suit) @rank = the_rank @suit = the_suit @symbols = [nil, nil, '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] end

我的代码有问题,似乎无法确定需要更改什么。这是我的三个文件,底部是我得到的错误。我有18次完全相同的代码,其中一半给了我这个错误

初始化等级、套装和符号

def initialize(the_rank, the_suit) 
  @rank = the_rank  
  @suit = the_suit  
  @symbols = [nil, nil, '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']  
end
新文件Pokerhand.rb: 需要/常量 需要/卡 需要/甲板 太空船

class PokerHand < Deck
  include Constants
  attr_reader :hand_type

  def initialize(the_cards)
    @cards = [ ]
    @hand_type = UNCLASSIFIED
  for card in the_cards
     @cards << card
  end
  end

    # Straight
  elsif @cards[0].rank == @cards[1]+1.rank &&
    @cards[1].rank == @cards[2]+1.rank &&
    @cards[2].rank == @cards[3]+1.rank &&
    @cards[3].rank == @cards[4]+1.rank   

    @hand_type = STRAIGHT

  end
 end
首先,您的PokerHandinitialize方法非常混乱。除了[]之外,您从未为@cards分配任何值,这是一个空数组。因此,当您调用分类方法时,@cards仍然是[],因此@cards[0]、@cards[1]或者实际上,@cards[x]对于任何x值都将始终为零。PokerHandinitialize应该更像这样:

def initialize(cards)
  @cards = cards
  @hand_type = UNCLASSIFIED
end

第二,你关于什么构成直接冲水的逻辑是不正确的。目前,只有当手中的每张牌都完全相同时,你才算是同花顺,例如,红桃三张牌的五份副本。

是@cards[3]。rank==@cards[4]+1.rank还是@cards[3]。rank==@cards[4]。rank=@cards[4]。rank=@cards[1两者都不是,因为排序后的牌会从最小到最大排序,所以它是@cards[3]。rank==@cards[4].rank-1I修复了我的代码,其中1/3是正确的,但其他的给出了相同的错误在许多系统中,这些卡表示为2D,3C。。。TH,其中T代表10,以使它们始终保持两个字母字符串。您还可以测试像这样的直接齐平:@cards.map&:suit.uniq.length==1,这意味着它们都有相同的西服。对于连续卡片,您应该按等级排序。如果你有五张完全相同的卡片,那么你的同花顺代码在这里工作的唯一方法就是。我修复了我的代码,其中1/3是好的,但是其他的给出了相同的错误1。你的代码中没有实现排名。这解决了问题谢谢!!
 TestClass#test_7:
NoMethodError: undefined method `rank' for 1:Fixnum
    PokerHand.rb:79:in `classify'
    test2.rb:76:in `test_7'
def initialize(cards)
  @cards = cards
  @hand_type = UNCLASSIFIED
end