From: "Brian F. Feldman" Date: 2000-09-26T09:10:56+09:00 Subject: [ruby-talk:5116] Re: Types and === > I imagine Yoda behind me, shaking his little green head > and saying, "You will never be a Jedi..." > > I know that the case statement uses the relationship operator ===; > and I know it's different for different objects; and I know it's not > commutative; and I know it's not ==. > > But still, one thing is a little counter-intuitive to me. > > Reason with me: Normally when x == y, x === y is also true (I'm > not saying the converse!!). But I have found a case where it isn't. > I can see there may be others.) You're right. This is definitely one such case. > See the fragment below, and its output. > > classify1 and classify2 don't do the same thing. The first fails; > the second works. classify3 also works. > > Comments, anyone? > > HF Try adding a little bit: > def classify1(arg) > case arg.type > when String > print " arg is a string\n" > when Array > print " arg is an array\n" > when Hash > print " arg is a hash\n" when Class print " arg is a class\n" > else > print " arg is unknown\n" > end > end You'll notice every example now says that " arg is a class\n". This is because the comparison you are doing in the case is defined to be (if not exactly, still the same operation): class Class def ===(other) return self == other.type end end So in your example, your operations evaluate to: if String === arg.type.type print " arg is a string\n" ... if Class == arg.type.type print "arg is a class\n" .... Since anything.type.type (unless you overload Anything#type evilly) will be Class, it doesn't do what you'd expect here. Hope this helps! -- Brian Fundakowski Feldman \ FreeBSD: The Power to Serve! / green@FreeBSD.org `------------------------------'