【问题标题】:Method behaving differently for 2 classes using same module method使用相同模块方法的 2 个类的方法表现不同
【发布时间】:2013-07-16 13:39:00
【问题描述】:

我之前在这里问过一个问题:I (think) I'm getting objects returned when I expect my array to have just 2 properties available 这可能会提供一些背景。收到解决方案后,我立即开始向每个玩家展示手牌。下面的模块包含在 Dealer 和 Player 类中。

module Hand
   def show_hand
      if self.class == Dealer
         #Need to cover up 1 of the 2 cards here. Dealer doesn't show both!
         print "The dealer is showing: "
         print self.hand[0].show_card
         puts ''
      elsif self.class == Player
         print "You have: "
         self.hand.each do |item|
            item.show_card
         end
         puts ''
      else
         puts "A Random person is showing their hand."
      end
   end
end

我知道这种定制违背了模块的目的,只是用它来强化模块的概念。上述解决方案适用于经销商部分。但是当调用 Player 部分时,它会打印一个空白块。在对 Player 块中的每个“项目”进行 .inspect 时,它确认这些项目实际上是预期的 Card 对象。这是之前的 show_card 方法:

def show_card
    "[#{@card_type} of #{@suit}]"
end

所以它只返回了一个带有 card_type 和花色的字符串。 只需将方法更改为此,我就能解决 Player 对象部分的问题:

def show_card
    print "[#{@card_type} of #{@suit}]"
end

为什么会这样?我假设它与玩家手上的“每个”的调用有关。真的只是好奇有什么区别以及为什么这些 Card 对象不会在没有显式“打印”的情况下打印出来,副通过 String 对象返回。

希望我的描述性足够。这让我感到困惑,我真的很想抓住这些小东西,因为我知道它会防止未来发生这样的错误。谢谢!

【问题讨论】:

    标签: ruby oop blackjack


    【解决方案1】:

    在 Ruby 中,您必须使用 putsprint 进行打印。仅返回字符串不会打印它。您的 Dealer 课程打印的原因是因为您执行了 print,但在您的 Player 课程中,正如您所指出的,您没有打印。您只返回了字符串而没有打印。

    正如您所说,您可以通过包含打印来修复它:

    def show_card
      print "[#{@card_type} of #{@suit}]"
    end
    

    你可以这样做:

    def show_card
      "[#{@card_type} of #{@suit}]"
    end
    
    ...
    
    module Hand
      def show_hand
        if self.class == Dealer
          #Need to cover up 1 of the 2 cards here. Dealer doesn't show both!
          print "The dealer is showing: "
          puts self.hand[0].show_card   # PRINT THE CARD
        elsif self.class == Player
          print "You have: "
          self.hand.each do |item|
            print item.show_card  # PRINT THE CARD
          end
          puts ''
        else
          puts "A Random person is showing their hand."
        end
      end
    end
    

    这会更“对称”一点,打印出你想要的东西。

    稍微紧凑一点的是:

    def show_card
      "[#{@card_type} of #{@suit}]"
    end
    
    ...
    
    module Hand
      def show_hand
        if self.class == Dealer
          #Need to cover up 1 of the 2 cards here. Dealer doesn't show both!
          puts "The dealer is showing: #{self.hand[0].show_card}"
        elsif self.class == Player
          print "You have: "
          self.hand.each { |item| print item.show_card }
          puts ''
        else
          puts "A Random person is showing their hand."
        end
      end
    end
    

    【讨论】:

    • 感谢您的回复。明显的错误。到目前为止,我并不是在这里提问的最佳途径。两次我都觉得我真的浏览了代码并且无法解释发生了什么。现在它完全有道理。信不信由你,发牌员卡并没有在模块中打印两次并在方法中打印。不知道为什么会发生这种情况,但至少这个谜团现在已经解决了。感谢您的快速回复!
    • @MikeWaldrup 实际上我弄错了双重打印。当您将print 放入show_card 类时,它不再返回字符串,而是返回print 本身产生的内容,即nil。所以你仍然只能得到它一次。我相应地更正了我的答案。
    猜你喜欢
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2014-10-25
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多