Files
game-scoring-scripts/card.rb
T
2008-08-28 18:52:35 -07:00

66 lines
1.0 KiB
Ruby

#!/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_s
case @rank
when 2..10 then @rank.to_s
when 11 then "Jack"
when 12 then "Queen"
when 13 then "King"
when 14 then "Ace"
end
end
def suit_s
@suit.to_s.capitalize
end
def to_s
rank_s + "-" + suit_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