Class: Card

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rank, suit) ⇒ Card

Returns a new instance of Card.



4
5
6
7
# File 'lib/playing_cards/card.rb', line 4

def initialize rank, suit 
  @rank = rank.to_s.capitalize
  @suit = suit.downcase.sub(/s$/,'').capitalize
end

Instance Attribute Details

#rankObject

Returns the value of attribute rank.



2
3
4
# File 'lib/playing_cards/card.rb', line 2

def rank
  @rank
end

#suitObject

Returns the value of attribute suit.



2
3
4
# File 'lib/playing_cards/card.rb', line 2

def suit
  @suit
end

Class Method Details

.parse(name) ⇒ Object Also known as: []



63
64
65
66
67
68
69
70
71
# File 'lib/playing_cards/card.rb', line 63

def self.parse name
  name = name.to_s.sub(/^the()?/i, '').strip

  if name =~ /^(\w+) of (\w+)$/i
    Card.new $1, $2
  elsif name =~ /^(\w+)of(\w+)$/i
    Card.new $1, $2
  end
end

Instance Method Details

#<(another_card) ⇒ Object



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

def < another_card
  numberify < another_card.numberify
end

#==(another_card) ⇒ Object



9
10
11
# File 'lib/playing_cards/card.rb', line 9

def == another_card
  rank == another_card.rank && suit == another_card.suit
end

#>(another_card) ⇒ Object



13
14
15
# File 'lib/playing_cards/card.rb', line 13

def > another_card
  numberify > another_card.numberify
end

#equals?(another_card) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/playing_cards/card.rb', line 21

def equals? another_card
  numberify == another_card.numberify
end

#inspectObject



57
58
59
# File 'lib/playing_cards/card.rb', line 57

def inspect
  "<Card: \"#{ name }\">"
end

#nameObject Also known as: to_s



53
54
55
# File 'lib/playing_cards/card.rb', line 53

def name
  "#{ rank } of #{ suit }s"
end

#numberifyObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/playing_cards/card.rb', line 25

def numberify
  convert = {
    'Two' => 2,
    'Three' => 3,
    'Four' => 4,
    'Five' => 5,
    'Six' => 6,
    'Seven' => 7,
    'Eight' => 8,
    'Nine' => 9,
    'Ten' => 10,
    'Jack' => 11,
    'Queen' => 12,
    'King' => 13,
    'Ace' => 14,
    '2' => 2,
    '3' => 3,
    '4' => 4,
    '5' => 5,
    '6' => 6,
    '7' => 7,
    '8' => 8,
    '9' => 9,
    '10' => 10
  }
  convert[rank]
end