Class: PokerHands::TwoPairs

Inherits:
PokerHand show all
Defined in:
lib/deck_of_cards_handler/poker_hands/two_pairs.rb

Constant Summary

Constants inherited from PokerHand

PokerHand::HANDS

Instance Attribute Summary collapse

Attributes inherited from PokerHand

#cards

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PokerHand

build_from_string, create

Constructor Details

#initialize(cards:) ⇒ TwoPairs

Returns a new instance of TwoPairs.



13
14
15
16
17
# File 'lib/deck_of_cards_handler/poker_hands/two_pairs.rb', line 13

def initialize(cards:)
  super
  @pairs = T.let(extract_pairs, T::Array[Card])
  @kicker = T.let(extract_kicker, T::Array[Card])
end

Instance Attribute Details

#kickerObject (readonly)

Returns the value of attribute kicker.



10
11
12
# File 'lib/deck_of_cards_handler/poker_hands/two_pairs.rb', line 10

def kicker
  @kicker
end

#pairsObject (readonly)

Returns the value of attribute pairs.



7
8
9
# File 'lib/deck_of_cards_handler/poker_hands/two_pairs.rb', line 7

def pairs
  @pairs
end

Class Method Details

.is?(cards) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/deck_of_cards_handler/poker_hands/two_pairs.rb', line 39

def is?(cards)
  counts = cards.map(&:rank).flatten.tally
  counts.values.count(2) == 2
end

Instance Method Details

#<=>(other) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/deck_of_cards_handler/poker_hands/two_pairs.rb', line 25

def <=>(other)
  return rank <=> other.rank unless instance_of?(other.class)

  first_pair_comparison = pair_values.first <=> other.pair_values.first
  return first_pair_comparison unless T.must(first_pair_comparison).zero?

  second_pair_comparison = pair_values.last <=> other.pair_values.last
  return second_pair_comparison unless T.must(second_pair_comparison).zero?

  kickers_value <=> other.kickers_value
end

#rankObject



20
21
22
# File 'lib/deck_of_cards_handler/poker_hands/two_pairs.rb', line 20

def rank
  3
end