Class: CardDeck::Card
- Inherits:
-
Object
- Object
- CardDeck::Card
- Defined in:
- lib/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) + (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 shorter representation of the card.
-
#black? ⇒ Boolean
Tests if the suit color is black.
-
#initialize(num, suit) ⇒ Card
constructor
Creates a new card.
Constructor Details
#initialize(num, suit) ⇒ Card
Creates a new card. Parameter num is the card’s number. Parameter suit is the card’s suit
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/card.rb', line 10 def initialize(num, suit) # Creates a new card. Parameter num is the card's number. Parameter suit is the card's suit unless SUIT.include? suit suit = case suit.downcase when "diamonds" then DIAMONDS when "spades" then SPADES when "hearts" then HEARTS when "clubs" then CLUBS end end @num, @suit = num, suit 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
8 9 10 |
# File 'lib/card.rb', line 8 def num @num end |
#suit ⇒ Object
The card’s suit. Must be Spades, Diamonds, Clubs, or Hearts.
9 10 11 |
# File 'lib/card.rb', line 9 def suit @suit end |
Class Method Details
Instance Method Details
#abbreviation ⇒ Object Also known as: abbr
The shorter representation of the card
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/card.rb', line 22 def abbreviation # The shorter representation of the card unless @num == "Joker" if @num == 10 then "#{@suit}#{@num}" else "#{@suit}#{(@num.to_s)[0]}" end else @num end end |
#black? ⇒ Boolean
Tests if the suit color is black
32 |
# File 'lib/card.rb', line 32 def black?; suit == SPADES || suit == CLUBS; end |