我正在尝试创建纸牌游戏的基础知识。在创建/测试初始卡组时,运行 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/