This solution tries to be elegant as possible but I am not in love with two aspects:
- The actual block that calculates the solution seems unnecessarily complex. Is there a way we can abstract some of the logic into generic methods like we did for Problem 3?
- Ruby’s
Rangeobject doesn’t support a reverse order. So10..1turns out to be equivalent to an empty array. This seems like an ugly wart of Ruby. We try to resolve the problem by sub-classingRangeas a new object calledReverseRangewhich works the opposite direction. I even monkey-patch a method calledreverseon theRangeobject which creates aReverseRangebased on the current range. This way we can still us the literal notation. I think(100..999).reverseis more elegant thanReverseRange.new 999, 100. But all this seems unnecessary. I kind of want to usedowntobut that doesn’t allow me tocollectorfind. Any other suggestions?