0

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
3
  • You expect us to count line numbers up to 30 and 73? Commented Aug 12, 2016 at 10:28
  • 3
    *cards.each do |card|cards.each do |card| Commented Aug 12, 2016 at 10:29
  • 1
    @mudasobwa Edited to include key line numbers. Thanks for the solution! Commented Aug 12, 2016 at 10:31

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.