class SimplePlayingCards::Card

Constants

RANKS
SUITS

Attributes

options[RW]
rank[R]
suit[R]
value[RW]

Public Class Methods

new(rank, suit, options = {}) click to toggle source
# File lib/simple_playing_cards/card.rb, line 10
def initialize(rank, suit, options = {})
  validate_inputs(rank, suit)
  @options = options
  @rank = rank
  @suit = suit
  @value = assign_value
end

Public Instance Methods

<=>(card) click to toggle source
# File lib/simple_playing_cards/card.rb, line 22
def <=> (card)
  if self.value < card.value
    -1
  elsif self.value > card.value
    1
  else
    0
  end  
end
name() click to toggle source
# File lib/simple_playing_cards/card.rb, line 18
def name
  "#{rank} of #{suit}"
end

Private Instance Methods

assign_value() click to toggle source
# File lib/simple_playing_cards/card.rb, line 34
def assign_value
  if rank.to_i > 0
    rank.to_i
  else
    case rank
    when 'Jack'
      11
    when 'Queen'
      12
    when 'King'
      13  
    when 'Ace'
      options['aces_high'] ? 14 : 1
    end
  end  
end
validate_inputs(rank, suit) click to toggle source
# File lib/simple_playing_cards/card.rb, line 51
def validate_inputs(rank, suit)
  errors = []
  errors << "Invalid argument type. Rank and Suit most both be strings!" unless rank.is_a?(String) && suit.is_a?(String)
  errors << "#{rank} is not a valid rank for a playing card!" unless RANKS.include?(rank)
  errors << "#{suit} is not a valid suit for a playing card!" unless SUITS.include?(suit)
  
  raise ArgumentError.new(errors.join(" ")) unless errors.empty?
end