← Back to archive

Stdle #22 · classes · 2026-06-28

What does this Ruby snippet output?

A past puzzle — fully playable. 4 attempts, hints on wrong guesses.

class Card
include Comparable
RANKS = %w[2 3 4 5 6 7 8 9 10 J Q K A]
attr_reader :rank
def initialize(r) = @rank = r
def <=>(other) = RANKS.index(rank) <=> RANKS.index(other.rank)
end
cards = [Card.new("K"), Card.new("2"), Card.new("A"), Card.new("7")]
puts cards.sort.map(&:rank).inspect
puts (Card.new("A") > Card.new("K"))
Classes
2 lines
4 attempts left

Answer & explanation