Ciaran S Ciaran S - 3 months ago 26
Ruby Question

Ruby syntax error, unexpected end-of-input, expecting keyword_end

I'm trying to create the basics of a card game. While creating/testing my initial deck I keep getting the following error message when I run my ruby code.

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


I looked up possible solutions and I can't seem to find my missing end. Could it possibly be something else? I'm very new to 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

Answer

You shouldn't use the splat operator inside the method, just keep it in arguments:

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