class TheGambler::Card

Constants

RANKS
SUIT_STRINGS
SUIT_SYMBOLS

Attributes

raw[R]

Public Class Methods

new(arg) click to toggle source

Initializer ————————————————————–

# File lib/the_gambler/card.rb, line 11
def initialize(arg)
  case arg.class.to_s
  when 'Card','TheGambler::Card'
    rank, suit = arg.rank, arg.suit
  when 'Fixnum'
    rank = RANKS[arg.zero? ? -1 : arg % 13]
    suit = SUIT_STRINGS[arg / 13]
  when 'String'
    if arg =~ /([ajqk2-9]|10)([SCHD])/i then
      rank, suit = $1.upcase, $2
    else
      raise ArgumentError.new("Invalid string: '#{arg}'")
    end
  when 'Array'
    if RANKS.include?(arg[0]) and SUIT_STRINGS.include?(arg[1]) then
      rank, suit = arg[0].upcase, arg[1]
    else
      raise ArgumentError.new("Invalid array: #{arg.inspect}")
    end
  when 'Hash'
    if RANKS.include?(arg[:rank]) and SUIT_STRINGS.include?(arg[:suit]) then
      rank, suit = arg[:rank].upcase, arg[:suit]
    else
      raise ArgumentError.new("Invalid hash: #{arg.inspect}")
    end
  else
    raise ArgumentError.new("Must supply either a String, an Array, or a Hash, not a #{arg.class.to_s}")
  end
  
  if SUIT_STRINGS.include?(suit)
    @raw = 13 * SUIT_STRINGS.index(suit) + RANKS.index(rank)
  elsif SUIT_SYMBOLS.include?(suit)
    @raw = 13 * SUIT_SYMBOLS.index(suit) + RANKS.index(rank)
  end
end

Public Instance Methods

<(other) click to toggle source

Instance methods ———————————————————

# File lib/the_gambler/card.rb, line 49
def <(other)
  return (@raw % 13) < (other.raw % 13)
end
<=>(other) click to toggle source
# File lib/the_gambler/card.rb, line 57
def <=>(other)
  return (@raw % 13) <=> (other.raw % 13)
end
>(other) click to toggle source
# File lib/the_gambler/card.rb, line 53
def >(other)
  return (@raw % 13) > (other.raw % 13)
end
ace?() click to toggle source
# File lib/the_gambler/card.rb, line 61
def ace?
  rank == 'A'
end
face_card?() click to toggle source
# File lib/the_gambler/card.rb, line 65
def face_card?
  %w{J Q K}.include?(rank)
end
numerical_value() click to toggle source
# File lib/the_gambler/card.rb, line 69
def numerical_value
  RANKS.index(rank) + 2
end
rank() click to toggle source
# File lib/the_gambler/card.rb, line 73
def rank
  RANKS[@raw % 13]
end
suit() click to toggle source
# File lib/the_gambler/card.rb, line 77
def suit
  SUIT_SYMBOLS[@raw / 13]
end
to_s() click to toggle source
# File lib/the_gambler/card.rb, line 81
def to_s
  "#{rank}#{SUIT_STRINGS[@raw / 13]}"
end