Class: Bridge::Card
Constant Summary collapse
- RANKS =
%w(2 3 4 5 6 7 8 9 10 J Q K A)- SUITS =
%w(C D H S)
Instance Attribute Summary collapse
-
#rank ⇒ Object
readonly
Returns the value of attribute rank.
-
#suit ⇒ Object
readonly
Returns the value of attribute suit.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #honour ⇒ Object
-
#initialize(rank, suit) ⇒ Card
constructor
A new instance of Card.
- #suit_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(rank, suit) ⇒ Card
Returns a new instance of Card.
10 11 12 13 14 15 |
# File 'lib/bridge/card.rb', line 10 def initialize(rank, suit) raise CardError.new "'#{rank}' is not a card rank" unless RANKS.include?(rank) raise CardError.new "'#{suit}' is not a card suit" unless SUITS.include?(suit) @rank = rank @suit = suit end |
Instance Attribute Details
#rank ⇒ Object (readonly)
Returns the value of attribute rank.
16 17 18 |
# File 'lib/bridge/card.rb', line 16 def rank @rank end |
#suit ⇒ Object (readonly)
Returns the value of attribute suit.
16 17 18 |
# File 'lib/bridge/card.rb', line 16 def suit @suit end |
Class Method Details
.from_string(string) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/bridge/card.rb', line 47 def self.from_string string raise CardError.new, "'#{string}' is not a card" if string.size < 2 suit = string[string.size-1] rank = string.chop new(rank.upcase, suit.upcase) end |
Instance Method Details
#<=>(other) ⇒ Object
18 19 20 21 22 |
# File 'lib/bridge/card.rb', line 18 def <=>(other) # this ordering sorts first by rank, then by suit (Card::SUITS.find_index(self.suit) <=> Card::SUITS.find_index(other.suit)).nonzero? or (Card::RANKS.find_index(self.rank) <=> Card::RANKS.find_index(other.rank)) end |
#honour ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bridge/card.rb', line 28 def honour case rank when 'J' 1 when 'Q' 2 when 'K' 3 when 'A' 4 else 0 end end |
#suit_i ⇒ Object
43 44 45 |
# File 'lib/bridge/card.rb', line 43 def suit_i SUITS.index(suit) end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/bridge/card.rb', line 24 def to_s @rank + @suit end |