Class: CardNine::Cards::PlayingCard

Inherits:
Object
  • Object
show all
Includes:
CardNine::Card, Comparable
Defined in:
lib/card_nine/cards/playing_card.rb

Overview

A class that represents the typical playing card from a 52 card deck.

Constant Summary collapse

SUITS =

ALL Suits of cards

%w|Clubs Hearts Diamonds Spades|.freeze
RANKS =

All Ranks of cards

%w|2 3 4 5 6 7 8 9 10 Jack Queen King Ace|.freeze

Instance Attribute Summary collapse

Attributes included from CardNine::Card

#id, #name

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rankString

Returns rank of card.

Returns:

  • (String)

    rank of card



26
# File 'lib/card_nine/cards/playing_card.rb', line 26

attribute :rank, String

#suitString

Returns suit of card.

Returns:

  • (String)

    suit of card



23
# File 'lib/card_nine/cards/playing_card.rb', line 23

attribute :suit, String

Class Method Details

.deck(rng = Random.new) ⇒ Object

create and return a standard 52 card deck of playing cards



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/card_nine/cards/playing_card.rb', line 34

def self.deck(rng = Random.new)
  all = []
  SUITS.each do |suit|
    RANKS.each do |rank|
      id = rank.to_i > 0 ? rank : rank[0]
      id += suit[0]
      all << { rank: rank, suit: suit, id: id, name: "#{rank} of #{suit}" }
    end
  end
  Deck.new(cards: all.map { |c| self.new(c) }, rng: rng)

end

Instance Method Details

#<=>(other) ⇒ Object

implement sort based on rank of the card, 2 low ace high



29
30
31
# File 'lib/card_nine/cards/playing_card.rb', line 29

def <=>(other)
  RANKS.find_index(rank) <=> RANKS.find_index(other.rank)
end