How does Integer === 3 work?

So, as I understand it, the === operator checks if the RHS object is a member of the LHS object. It makes sense. But how does it work in Ruby? I look at Ruby docs and I only see === defined in Object , I don't see it in Integer . Is it just not documented?

+4
source share
3 answers

Integer is a class that (at least in Ruby) means that it is just a boring old normal object, like any other object that is simply an instance of the Class class (instead of, say, Object or String or MyWhateverFoo ).

Class , in turn, is a subclass of Module (although this probably should not be, since it violates the Principle of Liskov by-laws, but this is a discussion for another forum, as well as a dead horse that has already been beaten many times). And in Module#=== you will find the definition you are looking for, which Class inherits from Module , and understand instances of Class (e.g. Integer ).

Module#=== is basically defined symmetrically Object#kind_of? , it returns true if its argument is an instance of itself. So 3 is an instance of Integer , so Integer === 3 returns true , just like 3.kind_of?(Integer) .

So, as I understand it, the === operator checks if the RHS object is a member of the LHS object.

Not necessary. === is a method, like any other method. He does what I want. And in some cases, the โ€œmemberโ€ of the โ€œanalogyโ€ breaks down. In this case, itโ€™s already pretty difficult to learn. If you are a hardcore type theory, then viewing the type as a set and instances of this type as members of the set is absolutely natural. And, of course, for Array and Hash definition of "member" is also obvious.

But what about Regexp ? Again, if you are a buff of official languages โ€‹โ€‹and know your Chomsky ago, then the interpretation of Regexp as an infinite set of words and String , since the members of this set feel quite natural, but if not, it sounds strange.

So far, I have not been able to make a brief description of what === means. In fact, I did not even come up with a good name. It is usually called the triple equality operator, the three-time operator or the case equality operator, but I strongly dislike these names because it has absolutely nothing to do with equality.

So what is he doing? The best thing I've come up with: imagine you are creating a table and one of the column headers is Integer . Does it make sense to write 3 in this column? If one of the column headings is /ab*a/ , does it make sense to write 'abbbba' in this column?

Based on this definition, it could be called the subsumption operator, but it is even worse than other examples ...

+8
source

It is defined in a module whose class is a subclass of which Integer is an instance.

In other words, when Integer === 3 starts, you call '===' (with parameter 3) on the object referenced by the Integer constant, which is an instance of a class called Class. Since the class is a subclass of the module and does not define its own === , you get the implementation === defined in the module.

See the API documents for the module for more information.

+3
source

Umm, Integer - a subclass of the object.

-one
source

All Articles