This tutorial generates a Ruby function to convert any low natural number into a Roman Numeral.
Writing the tutorial caused a couple false starts. TDD is no perfect algorithm generator. Like all hill-climbing algorithms, starting with the wrong abstraction can often make the right one harder, not easier, to climb to. I had to start three times, each time solving “II” a different way, before the algorithm clicked in.
The code to pass the test must be the simplest possible, but the refactoring, after the test passes, must then be very aggressive.
The refactoring here leveraged two simple rules: Make things look similar, then put them next to each other, then make them the same. That practice forms little tables, which eventually become real source tables.
Following those rules as closely as possible allowed a better algorithm to emerge.
The tutorial methods are brief enough that