So as I understand it, the === operator tests to see if the RHS object is a member of the LHS object. That makes sense. But how does this work in Ruby? I’m looking at the Ruby docs and I only see === defined in Object, I don’t see it in Integer itself. Is it just not documented?
So as I understand it, the === operator tests to see if the RHS
Share
Integeris a class, which (at least in Ruby) means that it is just a boring old normal object like any other object, which just happens to be an instance of theClassclass (instead of, say,ObjectorStringorMyWhateverFoo).Classin turn is a subclass ofModule(although arguably it shouldn’t be, because it violates the Liskov Substition Principle, but that is a discussion for another forum, and is also a dead horse that has already been beaten many many times). And inModule#===you will find the definition you are looking for, whichClassinherits fromModuleand instances ofClass(likeInteger) understand.Module#===is basically defined symmetric toObject#kind_of?, it returnstrueif its argument is an instance of itself. So,3is an instance ofInteger, thereforeInteger === 3returnstrue, just as3.kind_of?(Integer)would.Not necessarily.
===is a method, just like any other method. It does whatever I want it to do. And in some cases the “is member of” analogy breaks down. In this case it is already pretty hard to swallow. If you are a hardcore type theory freak, then viewing a type as a set and instances of that type as members of a set is totally natural. And of course forArrayandHashthe definition of “member” is also obvious.But what about
Regexp? Again, if you are formal languages buff and know your Chomsky backwards, then interpreting aRegexpas an infinite set of words andStrings as members of that set feels completely natural, but if not, then it sounds kind of weird.So far, I have failed to come up with a concise description of precisely what
===means. In fact, I haven’t even come up with a good name for it. It is usually called the triple equals operator, threequals operator or case equality operator, but I strongly dislike those names, because it has absolutely nothing to do with equality.So, what does it do? The best I have come up with is: imagine you are making a table, and one of the column headers is
Integer. Would it make sense to write3in that column? If one of the column headers is/ab*a/, would it make sense to write'abbbba'in that column?Based on that definition, it could be called the subsumption operator, but that’s even worse than the other examples …