Class: PokerRanking::Card

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

Constant Summary collapse

RANKS =
%w(2 3 4 5 6 7 8 9 10 Jack Queen King Ace)
SHORT_RANKS =
%w(2 3 4 5 6 7 8 9 10 J Q K A)
SUITS =
%w(Hearts Diamonds Spades Clubs)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card_data) ⇒ Card

Returns a new instance of Card.



9
10
11
12
13
# File 'lib/card.rb', line 9

def initialize(card_data)
  @suit = (card_data.has_key?(:suit) ? card_data[:suit] : card_data['suit']).capitalize
  @rank = card_data.has_key?(:rank) ? card_data[:rank].to_s : card_data['rank'].to_s
  set_value_by_rank_name(@rank)
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



37
38
39
# File 'lib/card.rb', line 37

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



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

def suit
  @suit
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.by_id(id) ⇒ Object



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

def self.by_id(id)
  PokerRanking::Card.new({rank: RANKS[id % 13], suit: SUITS[id / 13]})
end

.by_name(name) ⇒ Object



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

def self.by_name(name)
  rank, suit = name.split /\s+of\s+/
  PokerRanking::Card.new({rank: rank, suit: suit})
end

Instance Method Details

#==(other_card) ⇒ Object



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

def ==(other_card)
  @value == other_card.value && @rank == other_card.rank
end

#dataObject



28
29
30
# File 'lib/card.rb', line 28

def data
  { rank: SHORT_RANKS[@value - 2], suit: @suit.downcase }
end

#set_value_by_rank_name(rank) ⇒ Object



15
16
17
18
# File 'lib/card.rb', line 15

def set_value_by_rank_name(rank)
  index = RANKS.index(rank.capitalize) ? RANKS.index(rank.capitalize) : SHORT_RANKS.index(rank.capitalize)
  @value = index + 2
end

#to_sObject



24
25
26
# File 'lib/card.rb', line 24

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

#worth_less_than(other_hand) ⇒ Object



20
21
22
# File 'lib/card.rb', line 20

def worth_less_than(other_hand)
  @value < other_hand.value
end