From: "David A. Black" Date: 2010-08-23T08:15:16+09:00 Subject: Re: case and class Hi -- On Sat, 21 Aug 2010, Ralph Shnelvar wrote: > 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. It doesn't suggest the same functionality, though. This: case obj when XXX runs XXX === obj (as per Colin's explanation), and that examines whether or not XXX is in the method look-up path of obj. This may or may not have anything to do with obj's class -- for example: >> class C; end => nil >> module M; end => nil >> c = C.new.extend(M) => # >> case c >> when M; 1 >> end => 1 If you want to know whether a given class has a certain ancestor, you can do that too, but with a different technique: if C.ancestors.include?(B) etc. In general, the === mechanism is advantageous because it means you always know what's going on in a case statement, and because it gives you control over how your own objects behave in case statements. David -- David A. Black, Senior Developer, Cyrus Innovation Inc. The Ruby training with Black/Brown/McAnally Compleat Philadelphia, PA, October 1-2, 2010 Rubyist http://www.compleatrubyist.com