Class: CardDeck::Card

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#numObject

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

#suitObject

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

#abbreviationObject 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

Returns:

  • (Boolean)


31
# File 'lib/card.rb', line 31

def black?; suit == Spades || suit == Clubs; end

#red?Boolean

Tests if the suit color is red

Returns:

  • (Boolean)


32
# File 'lib/card.rb', line 32

def red?; suit == Hearts || suit == Diamonds; end