[Ruby] Equal is not always equal


The reason for this blog article is a question that dealt with the different ways of checking equality in Ruby, or more specifically the so called “threequals” operator method. You might have come across it, it is the three equals sign ===, a very Ruby specific thing. Even though everybody calls it operator, I think for the understanding it is crucial to be specific here – “threequals” is a method (of an object), not an operator. I stress this, as this is not true for other object oriented languages, such as Java, in which all infix operators (such as ==) are simply part of the language, i.e. their definition exists outside the object world.

After the before mentioned question I struggled myself of finding any good explanation of the different methods Ruby provides of comparing things, which is why I decided to write down the things I told him; and additionally place it into the context of all four methods, Ruby offers. Yes, there are four ways of comparison, which is twice as much as languages such as Java or Smalltalk offer (and think of C++ which just knows just the == comparison). To fully appreciate the differences, let me start of with the first and typical stumbling block, every novice programmer encounters: value equality vs. reference equality (skip this if you are familiar with the concept).

Continue reading

Advertisements