Sha256: 6f919242795e238ca3417ee6f8aafbdb4b86b43320b1474b3e41159185bc51ab

Contents?: true

Size: 915 Bytes

Versions: 2

Compression:

Stored size: 915 Bytes

Contents

module CardsLib
  class Card
    include Comparable
    def initialize(face, ranker = Ranker)
      raise InvalidCardFace, "face cannot be blank" if face.to_s.empty?
      @suit = nil
      @rank = nil
      @ranker = ranker
      if face.is_a? Hash
        @suit = face.fetch(:suit) { nil }
        @rank = face.fetch(:rank) { nil }
        if @rank && @suit
          str = ""
          if @rank.length.>(1) && @suit.length.>(1)
            str = " of "
          end
          @face = [@rank, str, @suit].join
        end
      else
        @face = face
      end
      @ranker = ranker.new(rank)
    end

    def face
      @face   
    end

    def suit
      @suit || @face[1..-1]
    end

    def rank
      @rank || @face[0]
    end

    def <=>(other)
      @ranker.<=>(other)
    end

    def sequential(other)
      @ranker.sequential(other)
    end
  end

  class InvalidCardFace < Exception

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cards_lib-0.0.8 lib/cards_lib/card.rb
cards_lib-0.0.7 lib/cards_lib/card.rb