[#4766] Wiki — "Glen Stampoultzis" <trinexus@...>

21 messages 2000/09/04
[#4768] RE: Wiki — "NAKAMURA, Hiroshi" <nahi@...> 2000/09/04

Hi, Glen,

[#4783] Re: Wiki — Masatoshi SEKI <m_seki@...> 2000/09/04

[#4785] Re: Wiki — "NAKAMURA, Hiroshi" <nakahiro@...> 2000/09/05

Howdy,

[#4883] Re-binding a block — Dave Thomas <Dave@...>

16 messages 2000/09/12

[#4930] Perl 6 rumblings -- RFC 225 (v1) Data: Superpositions — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/09/15

[#4936] Ruby Book Eng. translation editor's questions — Jon Babcock <jon@...>

20 messages 2000/09/16

[#5045] Proposal: Add constants to Math — Robert Feldt <feldt@...>

15 messages 2000/09/21

[#5077] Crazy idea? infix method calls — hal9000@...

This is a generalization of the "in" operator idea which I

17 messages 2000/09/22

[#5157] Compile Problem with 1.6.1 — Scott Billings <aerogems@...>

When I try to compile Ruby 1.6.1, I get the following error:

15 messages 2000/09/27

[ruby-talk:5116] Re: Types and ===

From: "Brian F. Feldman" <green@...>
Date: 2000-09-26 00:10:56 UTC
List: ruby-talk #5116
> <sigh> 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                    `------------------------------'



In This Thread