Class: CardDeck::Card
- Inherits:
-
Object
- Object
- CardDeck::Card
- Defined in:
- lib/card_deck.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) + (2..10).to_a
- SUIT =
Legal arguments for parameter suit in Card#new
[HEARTS, SPADES, DIAMONDS, CLUBS]
Instance Attribute Summary collapse
-
#num ⇒ Object
The card’s number.
-
#suit ⇒ Object
The card’s suit.
Class Method Summary collapse
-
.gen ⇒ Object
Creates a random new card.
Instance Method Summary collapse
-
#abbreviation ⇒ Object
(also: #abbr)
The short version of the card.
-
#initialize(num, suit = nil) ⇒ Card
constructor
Creates a new card.
Constructor Details
#initialize(num, suit = nil) ⇒ Card
Creates a new card. Parameter num is the card’s number. Parameter suit is the card’s suit
19 20 21 22 23 24 25 |
# File 'lib/card_deck.rb', line 19 def initialize(num, suit=nil) unless NUM.include?(num) || SUIT.include?(suit) || suit.nil? raise CardError, 'Illegal argument' else @num, @suit = num, suit end end |
Instance Attribute Details
#num ⇒ Object
The card’s number. Must be Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, or Joker
15 16 17 |
# File 'lib/card_deck.rb', line 15 def num @num end |
#suit ⇒ Object
The card’s suit. Must be Spades, Diamonds, Clubs, Hearts, or nil.
17 18 19 |
# File 'lib/card_deck.rb', line 17 def suit @suit end |
Class Method Details
.gen ⇒ Object
Creates a random new card.
26 27 28 |
# File 'lib/card_deck.rb', line 26 def self.gen # Creates a random new card. self.new NUM.sample, SUIT.sample end |
Instance Method Details
#abbreviation ⇒ Object Also known as: abbr
The short version of the card
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/card_deck.rb', line 29 def abbreviation # The short version of the card unless @num == "Joker" if @num == 10 then "#{@suit}#{@num}" else "#{@suit}#{(@num.to_s)[0]}" end else @num end end |