From: Ralph Shnelvar Date: 2010-08-21T08:13:22+09:00 Subject: Re: case and class ------------11099643AD405AB Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Colin, Friday, August 20, 2010, 4:29:37 PM, you wrote: CB> On Fri, Aug 20, 2010 at 10:51 PM, Ralph Shnelvar wrote: >> Why does "YYY" get printed out here? >> - - - >> class XXX >> end >> x = XXX.new >> case x.class >> when XXX >> puts "XXX" >> else >> puts "YYY" # surprise! >> end CB> It's because of the definition of "===" which is used in the case statement. CB> Does the following help? CB> class XXX CB> end CB> x = XXX.new CB> case x.class CB> when XXX then puts "XXX" CB> else puts "YYY" # surprise! end #=>> YYY CB> case x CB> when XXX then puts "XXX 2" CB> else puts "YYY 2" end #=>> XXX 2 CB> case x.class CB> when Class then puts "XXX 3" CB> else puts "YYY 3" end #=>> XXX 3 Colin, thank you. Very clear. Now it's my turn to complain about the unexpected behavior. 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? It just seems to me to be an unnecessary surprise to do things differently for objects of class Class. Indeed x.class == XXX # => true X.class _is_ XXX .... and yet the *when* does not pick it up because === is being used for comparison. Anyway, thank for your lucid and well-written response. ------------11099643AD405AB--