Sha256: a72e1a8f0487b04fb4d504fed7c9584bca8024c2b08ce1b96a1875794dd574d7

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 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]
    end

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

    # 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

    def to_str
      @card
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bridge-0.0.6 lib/bridge/card.rb