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 variablevariables (private)
@@suit_value = Hash[ * Suits.each_with_index.to_a.flatten]
@@value = {
@@pip_value = Hash[ * Pips.each_with_index.to_a.flatten]
"2" => 2, "3" => 3, "4" => 4, "5" => 5, "6" => 6,
"7" => 7, "8" => 8, "9" => 9, "10" => 10,
"Jack" => 11, "Queen" => 12, "King" => 13, "Ace" => 14,
}
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 @@value[@pip] <=> @@value[card.pip]\
@@pip_value[@pip] <=> @@pip_value[card.pip]
end
end
Anonymous user