← Back to archive

Stdle #136 · comparisons · 2026-10-20

What does this Ruby snippet output?

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

Rectangle = Struct.new(:width, :height) do
include Comparable
def <=>(other)
area <=> other.area
end
def area
width * height
end
end
r1 = Rectangle.new(2, 5)
r2 = Rectangle.new(3, 4)
puts r1 > r2
puts r1 < r2
puts [Rectangle.new(3,3), Rectangle.new(2,6), Rectangle.new(1,10)].min.to_a.inspect
Comparisons
3 lines
4 attempts left

Answer & explanation