Class: Bridge::Card
Instance Attribute Summary collapse
-
#card ⇒ Object
readonly
Returns the value of attribute card.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares the card with the other card.
- #coerce(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#honour_card_points ⇒ Object
(also: #hcp)
Returns the honour card points.
-
#initialize(card) ⇒ Card
constructor
Creates a new card.
- #inspect ⇒ Object
-
#suit ⇒ Object
Returns the suit of the card.
- #to_s ⇒ Object
-
#value ⇒ Object
Returns the suit of the card.
Constructor Details
Instance Attribute Details
#card ⇒ Object (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
46 47 48 |
# File 'lib/bridge/card.rb', line 46 def eql?(other) self == other && other.instance_of?(Card) end |
#hash ⇒ Object
50 51 52 |
# File 'lib/bridge/card.rb', line 50 def hash card.hash end |
#honour_card_points ⇒ Object 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 |
#inspect ⇒ Object
58 59 60 |
# File 'lib/bridge/card.rb', line 58 def inspect card.inspect end |
#suit ⇒ Object
Returns the suit of the card
14 15 16 |
# File 'lib/bridge/card.rb', line 14 def suit card[0] end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/bridge/card.rb', line 62 def to_s card end |
#value ⇒ Object
Returns the suit of the card
19 20 21 |
# File 'lib/bridge/card.rb', line 19 def value card[1] end |