ruby - Ruby语法错误,输入意外终止,应使用keyword_end

标签 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

我查找了可能的解决方案,但似乎找不到找不到的终点。可能是其他吗?我是 ruby 的新手。
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

最佳答案

您不应在方法内部使用splat运算符,而应将其保留在参数中:

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

关于ruby - Ruby语法错误,输入意外终止,应使用keyword_end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38915654/

相关文章:

ruby - 查询执行解释的巨大差异

python - 在不安装模块的情况下从 Python 2.4 运行 MySQL 查询 (MySQLdb)

php - PHP解析/语法错误;以及如何解决它们

ruby - 用 `Array.new(n, Array.new)` 创建矩阵

ruby - Ruby 可枚举的辅助 block

c++ - 堆变量作用域

javascript - JS - 未捕获的语法错误 : Unexpected Token

c# - 语法错误与 Visual Studio 中的编译器错误,或红色波浪下划线与蓝色波浪下划线

javascript - Bootstrap 模式 : Cannot call method 'scrollTop' of undefined

ruby - Sort_by Ruby,一个降序,一个升序