From: Ralph Shnelvar Date: 2010-08-21T13:06:10+09:00 Subject: Re: case and class ------------104EC133C025ABD Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Colin, Friday, August 20, 2010, 8:05:36 PM, you wrote: CB> On Sat, Aug 21, 2010 at 12:13 AM, Ralph Shnelvar wrote: >> Having read your explanation and Dave Thomas' *Programming Ruby 1.9* book >> about the use of === in comparisons in case statements ... >> Is it he fact that *case* uses == (two ='s) for non-Class *when's* and uses >> === (three ='s) for *when's* that are Classes? CB> No (I think the following is right, but I'd welcome any corrections) - case CB> always uses "===", it's just that the meaning of "===" may be different to CB> "==". CB> 1. case always uses "===" for comparisons; in CB> case x CB> when y then CB> the test is y === x CB> http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html CB> Common comparison operators, Operator Meaning CB> == Test for equal value. CB> === Used to test equality within a when clause of a case statement. CB> case operates by comparing the target (the expression after the keyword CB> case) with each of the comparison expressions after the when keywords. This CB> test is done using comparison === target. As long as a class defines CB> meaningful semantics for === (and all the built-in classes do), objects of CB> that class can be used in case expressions. CB> ... CB> Ruby classes are instances of class Class, which defines === as a test to CB> see if the argument is an instance of the class or one of its superclasses. CB> 2. http://www.ruby-doc.org/core/classes/Object.html#M000345 obj === other =>> true or false CB> Case Equality�For class Object, effectively the same as calling #==, but CB> typically overridden by descendents to provide meaningful semantics in case CB> statements. CB> 3. http://www.ruby-doc.org/core/classes/Module.html#M001666 CB> (remembering that Class is a subclass of Module, which I forget when I CB> initially couldn't find the "===" method in Class) mod === obj =>> true or false CB> Case Equality�Returns true if anObject is an instance of mod or one of mod�s CB> descendents. Of limited use for modules, but can be used in case statements CB> to classify objects by class. CB> Putting that all together, and using the String class as an example: CB> x = "Karel Capek" CB> y = "Karel Capek" p x.object_id #=>> 1880688 p y.object_id #=>> 1880656, so x & y are different objects p x == y #=>> true p x === y #=>> true, so for String objects "===" is the same as "==" CB> # at least for comparing String objects with String objects p x == String #=>> false p x === String #=>> false CB> p x.class == String #=> true CB> p String == String #=> true CB> p x.class === String #=> false CB> p String === String #=> false p String == x #=>> false p String === x #=>> true; this is what is being used in the case statement CB> # case x; when String then true; else false; end CB> p String == x.class #=> true CB> p String === x.class #=> false; same as String === String What a great answer. And, again, clear. I can, reluctantly, see the use of === in an if statement. To me, case x when XXX then ... end would be MUCH clearer as case x.class when XXX.arrayOfDecendants end Of course, the code above is illegal ... but would have been clearer ... at least to me. ------------104EC133C025ABD--