【问题标题】:Else without rescue is useless否则没有救援是没用的
【发布时间】:2017-03-03 23:38:27
【问题描述】:

我的代码有问题,似乎无法弄清楚我需要更改什么。这是我的三个文件,底部是我得到的错误。

require './PokerHand'
    require "./Constants"
    require 'minitest/autorun'

    class TestClass < MiniTest::Test
      include Constants

        def test_1
          arr1 = [Card.new(2, "S"), Card.new(3, "S"),
             Card.new(4, "S"), Card.new(5, "S"),
             Card.new(6, "S")]
          ph1 = PokerHand.new(arr1)
          ph1.classify
          assert_equal STRAIGHT_FLUSH, ph1.hand_type
        end 

       def test_2
        arr2 = [Card.new(9, "C"), Card.new(9, "S"),
             Card.new(9, "H"), Card.new(9, "D"),
             Card.new(11, "S")]
        ph2 = PokerHand.new(arr2)
        ph2.classify
        assert_equal FOUR_OF_A_KIND, ph2.hand_type
      end

      def test_3
        arr3 = [Card.new(4, "C"), Card.new(9, "S"),
             Card.new(9, "H"), Card.new(9, "D"),
             Card.new(9, "C")]
        ph3 = PokerHand.new(arr3)
        ph3.classify
        assert_equal FOUR_OF_A_KIND, ph3.hand_type
      end

新文件 Pokerhand.rb:

require "./Constants"
require "./Card"
require "./Deck"
require "./CardSpaceship"

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


# Determine hand type of PokerHand object.
def classify

  @cards.sort!

  # Straight flush 
  if @cards[0].rank == @cards[1].rank +1 &&
     @cards[1].rank == @cards[2].rank +1 &&
     @cards[2].rank == @cards[3].rank +1 &&
     @cards[3].rank == @cards[4].rank +1 &&  
     @cards[0].suit == @cards[1].suit &&
     @cards[1].suit == @cards[2].suit &&
     @cards[2].suit == @cards[3].suit &&
     @cards[3].suit == @cards[4].suit
     @hand_type = STRAIGHT_FLUSH
  end
end

新文件 test2.rb:

require './PokerHand'
require "./Constants"
require 'minitest/autorun'

class TestClass < MiniTest::Test
  include Constants

    def test_1
      arr1 = [Card.new(2, "S"), Card.new(3, "S"),
         Card.new(4, "S"), Card.new(5, "S"),
         Card.new(6, "S")]
      ph1 = PokerHand.new(arr1)
      ph1.classify
      assert_equal STRAIGHT_FLUSH, ph1.hand_type
    end 

出现错误:

TestClass#test_1:
PokerHand.rb:145: warning: else without rescue is useless
C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': JulianHansen_P5/PokerHand.rb:30: syntax error, unexpected tINTEGER, expecting keyword_then or ';' or '\n' (SyntaxError)
  if @cards[0].rank == @cards[1].rank +1 &&
                                        ^
PokerHand.rb:31: syntax error, unexpected tINTEGER, expecting keyword_end
    @cards[1].rank == @cards[2].rank +1 &&

【问题讨论】:

标签: ruby unit-testing error-handling


【解决方案1】:

if/else 语句设置不当会显示warning: else without rescue is useless 警告。

if true
  puts 'hi''
end # end shouldn't be here
else
  puts 'whoops'
end

您应该在您的代码中找到并更正它,尽管这不是导致致命错误的原因。

if 语句中的 +1s 需要空格或括号:

if @cards[0].rank == @cards[1].rank + 1 &&

if @cards[0].rank == (@cards[1].rank +1) &&

【讨论】:

    猜你喜欢
    • 2013-07-13
    • 2011-03-11
    • 2012-05-08
    • 2015-10-27
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    相关资源
    最近更新 更多