Class: Alucard::Card

Inherits:
Object
  • Object
show all
Defined in:
lib/alucard/card.rb

Constant Summary collapse

RANKS =
'A23456789TJQK'
SUITS =
'SHCD'
RANK_NAMES =
%w[Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King]
SUIT_NAMES =
%w[Spades Hearts Clubs Diamonds]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ Card

Returns a new instance of Card.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/alucard/card.rb', line 10

def initialize spec
  case spec
  when Fixnum
    @rank, @suit = spec % 13, spec / 13
  when /([#{RANKS}])([#{SUITS}])/
    @rank, @suit = RANKS.index($1), SUITS.index($2)
  when /(\w+) of (\w+)/
    r, s = $1.capitalize, $2.capitalize

    raise ArgumentError, "Not a rank name: '#{r}'" unless RANK_NAMES.include? r
    raise ArgumentError, "Not a suit name: '#{s}'" unless SUIT_NAMES.include? s

    @rank, @suit = RANK_NAMES.index(r), SUIT_NAMES.index(s)
  else
    raise ArgumentError, "Bad card spec: '#{spec}'"
  end
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



8
9
10
# File 'lib/alucard/card.rb', line 8

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



8
9
10
# File 'lib/alucard/card.rb', line 8

def suit
  @suit
end

Instance Method Details

#to_sObject



28
29
30
# File 'lib/alucard/card.rb', line 28

def to_s
  RANKS[@rank] + SUITS[@suit]
end