July 22, 2003
MarkBernstein.org
 

Comparison

One of the nice things about Ruby is that it's got an extra comparison operator. We're familiar with our old LISP friends EQ and EQUALS, which ask "are these two things the same object" and "do these two things have the same value, even if they're different objects." My new red pail is equal to yours because they're exactly the same thing, but it's not eq because it's not the same pail.

Ruby has another comparison operator, ===, which means (as I understand it) "the same thing for the purpose of argument." For example, thisObject === aClass if this object is a member of that class. This is nice semantic sugar. After all, how often do we need to compare two classes?

But it's more than sugar, as I just discovered the hard way in Tinderbox. We're polishing the new release. It's in good shape, but yesterday I noticed that something was not quite right with my weblog. Stray items were showing up in the RSS feed, and Aaron observed that my unique identifiers weren't unique. It all ties down to DateValue::equals, which I'd special-cased to make it easier to tell if two dates were on the same day.

The natural way to compare dates days "these dates are different" if they're more than a second apart. That makes sense here, but it's not what you expect if you're asking whether this note goes on Today's Page or not.

The special comparison made things better in many places, but slightly broke the XML engine. Not enough so it wouldn't run, just enough so that some dates leaked out of the weblog.

Another reason you're soaking in it is a good idea.