Class: Bridge::Card

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card) ⇒ Card

Creates a new card

Raises:

  • (ArgumentError)


8
9
10
11
# File 'lib/bridge/card.rb', line 8

def initialize(card)
  @card = card.to_s.upcase
  raise ArgumentError, "invalid card: #{card}" unless Bridge.card?(@card)
end

Instance Attribute Details

#cardObject (readonly)

Returns the value of attribute card.



5
6
7
# File 'lib/bridge/card.rb', line 5

def card
  @card
end

Instance Method Details

#<=>(other) ⇒ Object

Compares the card with the other card



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bridge/card.rb', line 30

def <=>(other)
  case other
  when Card
    raise ArgumentError, "comparing card of suit #{suit} with suit #{other.suit}" unless suit == other.suit
    Bridge.compare_cards(self.card, other.card)
  when String
    self <=> Card.new(other)
  else
    begin
      a, b = other.coerce(self)
      a <=> b
    rescue
    end
  end
end

#coerce(other) ⇒ Object



54
55
56
# File 'lib/bridge/card.rb', line 54

def coerce(other)
  [Card.new(other.to_s), self]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  self == other && other.instance_of?(Card)
end

#hashObject



50
51
52
# File 'lib/bridge/card.rb', line 50

def hash
  card.hash
end

#honour_card_pointsObject Also known as: hcp

Returns the honour card points



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

def honour_card_points
  (%w(J Q K A).index(value) || -1) + 1
end

#inspectObject



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

def inspect
  card.inspect
end

#suitObject

Returns the suit of the card



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

def suit
  card[0]
end

#to_sObject



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

def to_s
  card
end

#valueObject

Returns the suit of the card



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

def value
  card[1]
end