Sha256: a8f6789444a596119f08329df7210aa473d91e3a334b6c39c3c1a48042d560a7

Contents?: true

Size: 1.22 KB

Versions: 18

Compression:

Stored size: 1.22 KB

Contents

module Bridge
  class Card
    include Comparable

    attr_reader :card

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

    # Returns the suit of the card
    def suit
      card[0..0]
    end

    # Returns the suit of the card
    def value
      card[1..1]
    end

    # Returns the honour card points
    def honour_card_points
      (%w(J Q K A).index(value) || -1) + 1
    end
    alias :hcp :honour_card_points

    # Compares the card with the other card
    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

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

    def hash
      card.hash
    end

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

    def inspect
      card.inspect
    end

    def to_s
      card
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
bridge-0.1.4 lib/bridge/card.rb
bridge-0.1.3 lib/bridge/card.rb
bridge-0.1.2 lib/bridge/card.rb
bridge-0.1.1 lib/bridge/card.rb
bridge-0.1.0 lib/bridge/card.rb
bridge-0.0.26 lib/bridge/card.rb
bridge-0.0.25 lib/bridge/card.rb
bridge-0.0.24 lib/bridge/card.rb
bridge-0.0.23 lib/bridge/card.rb
bridge-0.0.22 lib/bridge/card.rb
bridge-0.0.21 lib/bridge/card.rb
bridge-0.0.20 lib/bridge/card.rb
bridge-0.0.19 lib/bridge/card.rb
bridge-0.0.18 lib/bridge/card.rb
bridge-0.0.17 lib/bridge/card.rb
bridge-0.0.16 lib/bridge/card.rb
bridge-0.0.15 lib/bridge/card.rb
bridge-0.0.14 lib/bridge/card.rb