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) ⇒ 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

#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

Class Method Details

.genObject

Creates a random new card.



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

def self.gen; self.new NUM.sample, SUIT.sample; end

Instance Method Details

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

Returns:

  • (Boolean)


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

def black?; suit == SPADES || suit == CLUBS; end