Class: CardDeck::Card
- Inherits:
-
Object
- Object
- CardDeck::Card
- Defined in:
- lib/card_deck/card.rb
Overview
The central part of any card game. It is what makes card games ‘Card’ games.
Constant Summary collapse
- Num =
Legal arguments for parameter num in Card#new.
%w(Ace King Queen Jack Joker) + Array(2..10)
- Suit =
Legal arguments for parameter suit in Card#new
[Hearts, Spades, Diamonds, Clubs]
Instance Attribute Summary collapse
-
#num ⇒ String, Fixnum
Must be Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, or Joker.
-
#suit ⇒ String
Must be spades, diamonds, clubs, or hearts.
Instance Method Summary collapse
-
#abbreviation ⇒ String
(also: #abbr, #inspect)
The shorter representation of the card.
- #black? ⇒ Boolean
-
#initialize(num = Num.sample, suit = Suit.sample) ⇒ Card
constructor
A new instance of Card.
- #red? ⇒ Boolean
Constructor Details
#initialize(num = Num.sample, suit = Suit.sample) ⇒ Card
Returns a new instance of Card.
15 16 17 18 |
# File 'lib/card_deck/card.rb', line 15 def initialize(num=Num.sample, suit=Suit.sample) suit = Card.const_get suit.capitalize unless Suit.include? suit @num, @suit = num, suit end |
Instance Attribute Details
#num ⇒ String, Fixnum
Returns must be Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, or Joker.
8 9 10 |
# File 'lib/card_deck/card.rb', line 8 def num @num end |
#suit ⇒ String
Returns must be spades, diamonds, clubs, or hearts.
9 10 11 |
# File 'lib/card_deck/card.rb', line 9 def suit @suit end |
Instance Method Details
#abbreviation ⇒ String Also known as: abbr, inspect
The shorter representation of the card
24 25 26 27 28 29 |
# File 'lib/card_deck/card.rb', line 24 def abbreviation unless @num == "Joker" if @num == 10 then @suit + @num.to_s else @suit + Array(@num.to_s).fetch(0) end else @num.to_s end end |
#black? ⇒ Boolean
30 31 32 |
# File 'lib/card_deck/card.rb', line 30 def black? # @return [Boolean] suit == Spades || suit == Clubs end |
#red? ⇒ Boolean
33 34 35 |
# File 'lib/card_deck/card.rb', line 33 def red? # @return [Boolean] suit == Hearts || suit == Diamonds end |