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.
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 = Num.sample, suit = Suit.sample) ⇒ Card
constructor
Creates a new card.
-
#red? ⇒ Boolean
Tests if the suit color is red.
Constructor Details
#initialize(num = Num.sample, suit = Suit.sample) ⇒ Card
Creates a new card. Parameter num is the card’s number. Parameter suit is the card’s suit. When given no arguments, the num and suit are randomly generated
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/card.rb', line 10 def initialize(num=Num.sample, suit=Suit.sample) # Creates a new card. Parameter num is the card's number. Parameter suit is the card's suit. When given no arguments, the num and suit are randomly generated 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 |
Instance Method Details
#abbreviation ⇒ Object Also known as: abbr
The shorter representation of the card
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/card.rb', line 21 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
31 |
# File 'lib/card.rb', line 31 def black?; suit == Spades || suit == Clubs; end |
#red? ⇒ Boolean
Tests if the suit color is red
32 |
# File 'lib/card.rb', line 32 def red?; suit == Hearts || suit == Diamonds; end |