Sha256: f209e82211c77c313d51d70d29e813546cae4721e0e0fc84b1495d14efa06e71

Contents?: true

Size: 925 Bytes

Versions: 1

Compression:

Stored size: 925 Bytes

Contents

module CardsLib
  class Ranker
    include Comparable
    attr_reader :rank, :rank_lookup
    # Initialize's arguments:
    # rank        - is whatever part of the object, or the object itself you choose to use.
    # ranks       - is a simple hash to lookup a cards value with the keys matching the rank object.
    # rank_lookup - is an optional Proc to redefine how you will lookup a cards value.
    def initialize(rank = nil, ranks = nil, rank_lookup = nil)
      @rank = rank
      @ranks = ranks
      @rank_lookup = rank_lookup
    end

    def ranks
      @ranks || Standard::RANKS
    end

    def ranker(rank_face = @rank)
      if @rank_lookup
        @rank_lookup.(rank_face)
      else
        ranks.index(rank_face).to_i + 1
      end
    end

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

    def sequential?(other)
      (ranker(rank) - ranker(other.rank)).abs == 1
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cards_lib-0.2.5 lib/cards_lib/ranker.rb