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



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

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

#==(other) ⇒ Object

equal by both rank and suit



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

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

#eql?(other) ⇒ Boolean

equal by value

Returns:

  • (Boolean)


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

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

#faceObject



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

def face
  @face   
end

#ordered?(other) ⇒ Boolean

returns other if true

Returns:

  • (Boolean)


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

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

#pair?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#paired?(other) ⇒ Boolean

return other if true

Returns:

  • (Boolean)


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

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

#rankObject



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

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

#sequential?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#suitObject



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

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

#suited?(other) ⇒ Boolean

return other if true

Returns:

  • (Boolean)


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

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

#valueObject



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

def value
  @ranker.ranker
end