Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>Integer</code> is 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 the <code>Class</code> class (instead of, say, <code>Object</code> or <code>String</code> or <code>MyWhateverFoo</code>).</p> <p><code>Class</code> in turn is a subclass of <code>Module</code> (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 in <code>Module#===</code> you will find the definition you are looking for, which <code>Class</code> inherits from <code>Module</code> and instances of <code>Class</code> (like <code>Integer</code>) understand.</p> <p><code>Module#===</code> is basically defined symmetric to <code>Object#kind_of?</code>, it returns <code>true</code> if its argument is an instance of itself. So, <code>3</code> is an instance of <code>Integer</code>, therefore <code>Integer === 3</code> returns <code>true</code>, just as <code>3.kind_of?(Integer)</code> would.</p> <blockquote> <p>So as I understand it, the <code>===</code> operator tests to see if the RHS object is a member of the LHS object.</p> </blockquote> <p>Not necessarily. <code>===</code> 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 for <code>Array</code> and <code>Hash</code> the definition of "member" is also obvious.</p> <p>But what about <code>Regexp</code>? Again, if you are formal languages buff and know your Chomsky backwards, then interpreting a <code>Regexp</code> as an infinite set of words and <code>String</code>s as members of that set feels completely natural, but if not, then it sounds kind of weird.</p> <p>So far, I have failed to come up with a concise description of <em>precisely</em> what <code>===</code> means. In fact, I haven't even come up with a good name for it. It is usually called the <em>triple equals operator</em>, <em>threequals operator</em> or <em>case equality operator</em>, but I strongly dislike those names, because it has absolutely nothing to do with equality.</p> <p>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 <code>Integer</code>. Would it make sense to write <code>3</code> in that column? If one of the column headers is <code>/ab*a/</code>, would it make sense to write <code>'abbbba'</code> in that column?</p> <p>Based on that definition, it could be called the <em>subsumption operator</em>, but that's even worse than the other examples ...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload