class Bridge::Card

Constants

RANKS
SUITS

Attributes

rank[R]
suit[R]

Public Class Methods

from_string(string) click to toggle source
# 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
new(rank, suit) click to toggle source
# 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

Public Instance Methods

<=>(other) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/bridge/card.rb, line 43
def suit_i
  SUITS.index(suit)
end
to_s() click to toggle source
# File lib/bridge/card.rb, line 24
def to_s
  @rank + @suit
end