Anonymous user
Playing cards: Difference between revisions
m
→{{header|Ruby}}: don't need to hard-code class variables, calculate them
m (→{{header|Ruby}}: some improvements and sample output) |
m (→{{header|Ruby}}: don't need to hard-code class variables, calculate them) |
||
Line 1,069:
Pips = ["2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"]
# class
@@suit_value = Hash[ * Suits.each_with_index.to_a.flatten]
@@pip_value = Hash[ * Pips.each_with_index.to_a.flatten]
attr_reader :pip, :suit
Line 1,089 ⟶ 1,086:
# allow sorting an array of Cards: first by suit, then by value
def <=>(card)
(@@suit_value[@suit] <=> @@suit_value[card.suit]).nonzero? or
@@pip_value[@pip] <=> @@pip_value[card.pip]
end
end
|