class CardsLib::Card

Public Class Methods

new(face, ranker = Ranker) click to toggle source
# 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(rank)
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/cards_lib/card.rb, line 48
def <=>(other)
  @ranker.<=>(other)
end
==(other) click to toggle source

equal by both rank and suit

# File lib/cards_lib/card.rb, line 39
def ==(other)
  pair?(other) && suit == other.suit
end
eql?(other) click to toggle source

equal by value

# File lib/cards_lib/card.rb, line 44
def eql?(other)
  value == other.value
end
face() click to toggle source
# File lib/cards_lib/card.rb, line 18
def face
  @face
end
inspect() click to toggle source
# File lib/cards_lib/card.rb, line 14
def inspect
  "(Card)"
end
ordered?(other) click to toggle source

returns other if true

# File lib/cards_lib/card.rb, line 67
def ordered?(other)
  sequential?(other) ? other : nil
end
pair?(other) click to toggle source
# File lib/cards_lib/card.rb, line 34
def pair?(other)
  rank == other.rank
end
paired?(other) click to toggle source

return other if true

# File lib/cards_lib/card.rb, line 57
def paired?(other)
  (rank == other.rank) ? other : nil
end
rank() click to toggle source
# File lib/cards_lib/card.rb, line 26
def rank
  @rank || @face[0]
end
sequential?(other) click to toggle source
# File lib/cards_lib/card.rb, line 52
def sequential?(other)
  @ranker.sequential?(other)
end
suit() click to toggle source
# File lib/cards_lib/card.rb, line 22
def suit
  @suit || @face[1..-1]
end
suited?(other) click to toggle source

return other if true

# File lib/cards_lib/card.rb, line 62
def suited?(other)
  (suit == other.suit) ? other : nil
end
value() click to toggle source
# File lib/cards_lib/card.rb, line 30
def value
  @ranker.ranker
end

Private Instance Methods

face_from_rank_and_suit(rank, suit) click to toggle source
# File lib/cards_lib/card.rb, line 72
def face_from_rank_and_suit(rank, suit)
  if rank && suit
    [rank, ((rank.length.>(1) && suit.length.>(1)) ? " of " : ""), suit].join
  else
    raise InvalidRankAndSuit, "Suit and Rank provided in Hash are invalid!"
  end
end
if_hash_then_fetch(item, target) click to toggle source
# File lib/cards_lib/card.rb, line 80
def if_hash_then_fetch(item, target)
  item.is_a?(Hash) ? item.fetch(target) { nil } : nil
end