2016-08-12 123 views
0

我正在尝试创建纸牌游戏的基础知识。在创建/测试我的初始套件时,当我运行我的Ruby代码时,我不断收到以下错误消息。Ruby语法错误,意外的输入结束,期待keyword_end

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 
+0

您希望_us数行数多达30 73_? – mudasobwa

+3

'* cards.each do | card |''⇒'cards.each do | card |' – mudasobwa

+1

@mudasobwa编辑包含关键线号码。感谢您的解决方案! –

回答

1

你不应该使用图示操作的方法中,只要保持它的参数:

def add_cards(*cards) 
    cards.each do |card| 
     @cards << card 
    end 
end 
相关问题