Class: Card

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/deck/card.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ran = 'Joker', suit = 'None') ⇒ Card



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/deck/card.rb', line 7

def initialize(ran = 'Joker', suit = 'None')
  #TODO clean this up
  ran = "Jack" if ran == 11
  ran = "Queen" if ran == 12
  ran = "King" if ran == 13
  ran = "Ace" if ran == 14
  suit = "Hearts" if suit == "H"
  suit = "Clubs" if suit == "C"
  suit = "Spades" if suit == "S"
  suit = "Diamonds" if suit == "D"
  @suit = suit
  @rank = ran
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



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

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



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

def suit
  @suit
end

Instance Method Details

#+(num) ⇒ Object



41
42
43
# File 'lib/deck/card.rb', line 41

def + (num)
  Card.new(rank.to_i + num.to_i, suit)
end

#-(num) ⇒ Object



45
46
47
# File 'lib/deck/card.rb', line 45

def - (num)
  Card.new(rank.to_i - num.to_i, suit)
end

#<(other) ⇒ Object



62
63
64
# File 'lib/deck/card.rb', line 62

def <(other)
  to_i<other.to_i
end

#<=>(other) ⇒ Object



58
59
60
# File 'lib/deck/card.rb', line 58

def <=>(other)
  to_i<=>other.to_i
end

#==(other) ⇒ Object



54
55
56
# File 'lib/deck/card.rb', line 54

def ==(other)
  to_i == other.to_i && suit == other.suit
end

#>(other) ⇒ Object



66
67
68
# File 'lib/deck/card.rb', line 66

def >(other)
  to_i>other.to_i
end

#eachObject



70
71
72
# File 'lib/deck/card.rb', line 70

def each
  yield
end

#inspectObject



33
34
35
# File 'lib/deck/card.rb', line 33

def inspect
  to_s
end

#to_iObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/deck/card.rb', line 74

def to_i
  case @rank.to_s.upcase[0..1]      
    when 'JA'
      11
    when 'QU' 
      12
    when 'KI' 
      13
    when 'AC' 
      14
    when 'JO' 
      15
    else @rank.to_i  
  end  
end

#to_sObject



50
51
52
# File 'lib/deck/card.rb', line 50

def to_s
  "#{rank} of #{suit}"
end