From: "Brian F. Feldman" Date: 2000-09-26T12:31:33+09:00 Subject: [ruby-talk:5123] Re: Types and === > > ----- Original Message ----- > From: Yasushi Shoji > To: ruby-talk ML > Sent: Monday, September 25, 2000 7:25 PM > Subject: [ruby-talk:5120] Re: Types and === > > > > '===' does not imply '==' at all. '===' is defined in each class the > > way to make writing case statement easier. most classes use '==' for > > '===', but some class, such as class Class and class Range uses > > other.is_a?(self) and self.include?(other), respectively. > > > > Yes... I guess one root of my difficulty is that I have chosen to understand > Ruby's case statement in terms of the switch statement in C/C++ or the > case statement in Pascal (among others). > > But now I think that Ruby's case statement is not really like any other > multiway branch I have seen in any other language. Right! That's because Ruby is purely OO =) > > > Actually I have trouble remembering what === means -- > > > whether to say x === y or y === x, which are of course > > > not the same. > > > > so, i guess you don't have to remember what '===' at all ;) > > > > I think the moment you need to remember about '===' is when you write > > your own class. you can define '===' in your own class to control > > case statement. > > Yes, I guess that is true... but I do like to have a thorough understanding. > > But sometimes I struggle with the case statement because of the lack of > commutativity or symmetry. (Probably there are good reasons === is not > symmetrical. I have not thought about it much.) Well, it can't be symmetrical if operators are done as real OO (which, except for '=' and ',', they are -- just methods). > For example, if I want to ask, "Which pattern does this string match?" I can > easily do something like: > > case mystring > when /pat1/ > stmt1 > when /pat2/ > stmt > ... > end > > But to ask the question, "Which of these strings matches this pattern?" I > cannot > do this: > > case /pattern/ > when str1 > stmt1 > when str2 > stmt2 > ... > end So extend Ruby :) Like this: class String private OldEEE = method :=== public def ===(other) if other.is_a? Regexp return other === self end return OldEEE.call other end end > Hal -- Brian Fundakowski Feldman \ FreeBSD: The Power to Serve! / green@FreeBSD.org `------------------------------'