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

Class Method Summary collapse

Instance Method Summary collapse

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/card.rb', line 20

def initialize(num, suit=nil)
	unless suit.nil?
		suit = case suit.downcase
			when "diamonds" then DIAMONDS
			when "spades" then SPADES
			when "hearts" then HEARTS
			when "clubs" then CLUBS
		end
	end
	unless NUM.include?(num) || SUIT.include?(suit) || suit.nil?
	raise CardError, 'Illegal argument'
	else
		@num, @suit = num, suit
	end
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



16
17
18
# File 'lib/card.rb', line 16

def num
  @num
end

#suitObject

The card’s suit. Must be Spades, Diamonds, Clubs, Hearts, or nil.



18
19
20
# File 'lib/card.rb', line 18

def suit
  @suit
end

Class Method Details

.genObject

Creates a random new card.



35
36
37
# File 'lib/card.rb', line 35

def self.gen # Creates a random new card.
	self.new NUM.sample, SUIT.sample
end

Instance Method Details

#abbreviationObject Also known as: abbr

The shorter representation of the card



38
39
40
41
42
43
44
45
46
47
# File 'lib/card.rb', line 38

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)


48
49
50
# File 'lib/card.rb', line 48

def black? # Tests if the suit color is black
	suit == SPADES || suit == CLUBS
end