0

For a pack of playing cards:

How can I use the suit hash (below) when creating a pack?

I have:

class PackOfCards

  SUITS={H: 'Hearts', S:'Spades', D:'Diamonds', C:'Clubs'}
  CARDS=['A','2','3','4','5','6','7','8','9','10','J','Q','K']


  attr_accessor :pack_name, :cards

  def initialize(pack_name)

    @pack_name= pack_name
    @cards = []
    (0..3).each do |suit|
      (0..12).each do |number|
        @cards << PlayingCard.new(self, (SUITS[suit].value), CARDS[number])
      end 
    end 
  end 
end

class PlayingCard

  attr_accessor :pack, :card_number, :card_suit


  def initialize(pack, suit, number)
    @card_suit = suit
    @card_number = number
  end 

end

but I get:

pack_of_cards.rb:16:in `block (2 levels) in initialize': 
undefined method `value' for 
{:H=>"Hearts", :S=>"Spades", :D=>"Diamonds", :C=>"Clubs"}:Hash (NoMethodError)
2
  • SUITS is invalid Ruby expression. Commented Mar 17, 2013 at 16:57
  • Updated to use hash's {} syntax (not []) but same error :( Commented Mar 17, 2013 at 17:01

4 Answers 4

2

Your SUITS is invalid expression. Perhaps you wanted to do this:

SUITS = %w[Hearts Spades Diamonds Clubs]

And it is not clear what you are doing, but perhaps you should be doing this:

@cards =
SUITS.flat_map{|suit| CARDS.map{|number| PlayingCard.new(self, suit, number)}}
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a corrected version, check the comments :

class PackOfCards
  SUITS={H: 'Hearts', S:'Spades', D:'Diamonds', C:'Clubs'} # Use curly braces to define a hash, [] braces will define an array containing one hash
  CARDS=['A','2','3','4','5','6','7','8','9','10','J','Q','K']
  attr_accessor :pack_name, :cards

  def initialize(pack_name)
    @pack_name= pack_name
    @cards = []
    SUITS.each_key do |suit| # each_key is better since it gives you the key of the hash
      (0..12).each do |number|
        puts PackOfCards::SUITS[suit]
        @cards << PlayingCard.new(self, (PackOfCards::SUITS[suit]), PackOfCards::CARDS[number]) # Call the hash with the right key to get the Suit
      end
    end
  end
end

class PlayingCard 
  attr_accessor :pack, :card_number, :card_suit

  def initialize(pack, suit, number)
    @card_suit = suit
    @card_number = number
  end

end

2 Comments

Yes, I like it. I changed the PackOfCards::SUITS[suit] expression to SUITS[suit] as it is within the class. I welcome thoughts on that change :)
btw (others) I am using a hash rather than an array (which I used initially) because ultimately I want be to able to use either the one letter suit name or the full description. Right now I could also just use an array and grab the capitalized first letter, but this is homework (that I set myself) and wanted to see how to use a hash in this context.
0

Your Suit definition and lookup don't look valid.

How about something like this (assuming the output is a pack of cards with all suit and numbers) -

class PackOfCards

  SUITS = ['Hearts', 'Spades', 'Diamonds', 'Clubs']
  CARDS=['A','2','3','4','5','6','7','8','9','10','J','Q','K']


  attr_accessor :pack_name, :cards

  def initialize(pack_name)

    @pack_name= pack_name
    @cards = []
    (0..3).each do |suit|
      (0..12).each do |number|
        @cards << PlayingCard.new(self, (SUITS[suit]), CARDS[number])
      end 
    end 
  end 
end

class PlayingCard

  attr_accessor :pack, :card_number, :card_suit


  def initialize(pack, suit, number)
    @card_suit = suit
    @card_number = number
  end 

end

Comments

0

You've actually put a hash in an array. To access the key, value pairs you'd have to access the array element first like this:

SUITS.first[:H]

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.