#!/usr/bin/env ruby class Card attr_reader :rank, :suit def initialize(str) r = str[0,1].downcase s = str[1,1].downcase @suit = case s when 's' then :spades when 'h' then :hearts when 'd' then :diamonds when 'c' then :clubs end @rank = case r when '2'..'9' then r.to_i when 't' then 10 when 'j' then 11 when 'q' then 12 when 'k' then 13 when 'a' then 14 end end def rank_long_s ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace'][@rank-2] end def suit_long_s @suit.to_s.capitalize end def rank_s ['2','3','4','5','6','7','8','9','t','j','q','k','a'][@rank-2] end def suit_s {:spades => 's', :hearts => 'h', :diamonds => 'd', :clubs => 'c'}[@suit] end def to_s rank_s + suit_s end def to_long_s rank_long_s + '-' + suit_long_s end def ==(other) @rank == other.rank && @suit == other.suit end def <=>(other) if @rank != other.rank @rank <=> other.rank else # order is Spades, Hearts, Diamonds, Clubs (conveniently reverse alphabetical) other.suit_s <=> suit_s end end def eql?(other) self == other end def hash to_s.hash end end