Ruby语法错误,意外的输入结束,期待keyword_end

时间:2016-08-12 10:24:04

标签: ruby syntax-error

我试图创建纸牌游戏的基础知识。在创建/测试我的初始套牌时,我在运行我的ruby代码时不断收到以下错误消息。

gofish.rb:30: syntax error, unexpected '\n', expecting :: or '[' or '.'
gofish.rb:73: syntax error, unexpected end-of-input, expecting keyword_end
deck.add_cards

我查找了可能的解决方案,但我似乎无法找到我遗漏的结尾。它可能是别的吗?我对红宝石很新。

class Deck

    def initialize
        @ranks = %w(2 3 4 5 6 7 8 9 10 Jack Queen King Ace)
        @suits = %w(Clubs Spades Hearts Diamonds)
        @cards = []

        @ranks.each do |rank|
            @suits.each do |suit|
                @cards << Card.new(rank, suit)
            end
        end
    end

    def shuffle
        @cards.shuffle
    end

    def deal
        @cards.shift
    end

    def empty?
        @cards.empty?
    end

    def add_cards(*cards)
        *cards.each do |card|
            @cards << card
        end #line 30
    end

    def to_s
        output = ""

        @cards.each do |card|
            output = output + card.to_s + "\n"
        end

        return output
    end
end

class Hand

    def initialize
    end

    def search()
    end
end

class Card

    attr_reader :rank, :suit

    def initialize(rank, suit)
        @rank = rank
        @suit = suit
    end

    def to_s
        "#{@rank} of #{@suit}"
    end
end

deck = Deck.new

puts deck.to_s
deck.shuffle
puts deck.to_s
deck.deal
deck.add_cards #line 73

1 个答案:

答案 0 :(得分:1)

您不应该在方法中使用splat运算符,只需将其保存在参数中:

def add_cards(*cards)
    cards.each do |card|
        @cards << card
    end
end