← Back to archive

Stdle #55 · classes · 2026-07-31

What does this Ruby snippet output?

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

class Weight
include Comparable
def initialize(kg) = @kg = kg
def <=>(other) = @kg <=> other.instance_variable_get(:@kg)
def to_s = "#{@kg}kg"
end
w = [Weight.new(10), Weight.new(5), Weight.new(8)]
puts w.sort.map(&:to_s).inspect
puts w.min.to_s
Classes
2 lines
4 attempts left

Answer & explanation