Class: CardsLib::Card

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/cards_lib/card.rb

Instance Method Summary collapse

Constructor Details

#initialize(face, ranker = Ranker) ⇒ Card

Returns a new instance of Card.

Raises:



4
5
6
7
8
9
10
11
12
# File 'lib/cards_lib/card.rb', line 4

def initialize(face, ranker = Ranker)
  raise InvalidCardFace, "Parameter face cannot be blank!" if face.to_s.empty?
  @suit = if_hash_then_fetch(face, :suit)
  @rank = if_hash_then_fetch(face, :rank)
  @face = face_from_rank_and_suit(@rank, @suit) if face.is_a? Hash
  
  @face ||= face
  @ranker = ranker.new(self.rank)
end

Instance Method Details

#<=>(other) ⇒ Object



48
49
50
# File 'lib/cards_lib/card.rb', line 48

def <=>(other)
  @ranker.<=>(other)
end

#==(other) ⇒ Object

equal by both rank and suit



39
40
41
# File 'lib/cards_lib/card.rb', line 39

def ==(other)
  pair?(other) && self.suit == other.suit
end

#eql?(other) ⇒ Boolean

equal by value

Returns:

  • (Boolean)


44
45
46
# File 'lib/cards_lib/card.rb', line 44

def eql?(other)
  value == other.value
end

#faceObject



18
19
20
# File 'lib/cards_lib/card.rb', line 18

def face
  @face   
end

#inspectObject



14
15
16
# File 'lib/cards_lib/card.rb', line 14

def inspect
  "(Card)"
end

#ordered?(other) ⇒ Boolean

returns other if true

Returns:

  • (Boolean)


67
68
69
# File 'lib/cards_lib/card.rb', line 67

def ordered?(other)
  self.sequential?(other) ? other : nil
end

#pair?(other) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/cards_lib/card.rb', line 34

def pair?(other)
  self.rank == other.rank
end

#paired?(other) ⇒ Boolean

return other if true

Returns:

  • (Boolean)


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

def paired?(other)
  (self.rank == other.rank) ? other : nil
end

#rankObject



26
27
28
# File 'lib/cards_lib/card.rb', line 26

def rank
  @rank || @face[0]
end

#sequential?(other) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cards_lib/card.rb', line 52

def sequential?(other)
  @ranker.sequential?(other)
end

#suitObject



22
23
24
# File 'lib/cards_lib/card.rb', line 22

def suit
  @suit || @face[1..-1]
end

#suited?(other) ⇒ Boolean

return other if true

Returns:

  • (Boolean)


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

def suited?(other)
  (self.suit == other.suit) ? other : nil
end

#valueObject



30
31
32
# File 'lib/cards_lib/card.rb', line 30

def value
  @ranker.ranker
end