More Ruby Dating

In any/all programming languages dates and times are especially troublesome. The logical, orderly manner in which computers operate is almost in contrast to the way we humans conceptualize the world.
Our peculiar system of tracking time - 60 minutes for an hour among 24 in a day and 30 days (more or less) in a month and twelve months in the year which we break into four quarterly seasons - is truly arbitrary and, as such, complex for computers to handle.

Our peculiar system of tracking time is complex for computers to handle.

Again, working in Rails on Disciplr, I am dealing with dates. In this particular context it is specifically years and their quarters. I need to calculate the "change" from one quarter to another; both up and down. This sounds very simple to you and me. For a computer it is extremely complicated. Dealing with 1 through 4 (not zero-based) and incrementing or decrementing years as the quarters cross beyond 1 or 4 is tough to code.

When analyzing this problem I was struck by a symmetry in the system, though. If we reduce ourselves to the numbering as quaternary the math is easy and the problem is greatly simplified!

There is not a direct class or utility in Ruby to handle this but, the little known side-effect of to_i and to_s allows representing numbers in any system up to 36.

Ruby to_s

Admittedly this is a bit awkward since the conversion exists only on the to_s and to_i methods.
But the fact that the roll-over from quarter to year occurs in base 4 allows a simpler approach to the problem. Let's not forget that Base-4 consists of 0, 1, 2, 3. So an adjustment must be made to accomodate the "human" concept of quarters 1-4. Fortunately, handling zero-based numbering is natural for computers.

I wrote the code and it passed my tests so the immenent problem has been solved*.

I have plans to encapsulate all this into a class. I will share that when it is completed; or perhaps build a gem out of it.


*One reason I so enjoy writing software is that it involves solving puzzles. Many of the puzzles are very simple - some are straightforward. It is especially gratifying when you can devise a creative and effective solution to a problem.