[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: surprising: class A; end; A === A ==> false

Jason Bailey

2/23/2005 5:23:00 PM

2 Answers

Jamis Buck

2/23/2005 5:37:00 PM

0

On 02:22 Thu 24 Feb , Jason Bailey wrote:
> I'm thinking you can get what you want by
>
> case t
> when A.class then...
> when B.class then...
> end
>
> since you want to compare the actual class of the object

The problem is that A.class == B.class == Class. Apparently, the case
statement is really just not designed for comparing class objects.

- Jamis

> > -------- Original Message --------
> > Subject: surprising: class A; end; A === A ==> false
> > From: "Sam Roberts" <sroberts@uniserve.com>
> > Date: Wed, February 23, 2005 11:24 am
> > To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
> >
> > I'm used to thinking of === being MORE useful
> > On thinking about it, I can see why: in A === A, A is of class Class,
> > and that class is not derived from the class A, so comparison is
> > false...
> >
> > But this causes me some trouble, because there is no way to use
> > case statements with a class:
> >
> > class A
> > Ordinal = 1
> > end
> > class B
> > Ordinal = 2
> > end
> >
> > t = A
> >
> > case t
> > when A then ...
> > when B then ...
> > end
> >
> > No case will ever match!
> >
> > Is my only way:
> >
> > if t == A
> > ...
> > elsif t == B
> > ...
> > elsif
> > .....
> >
> > or is there some clever workaround?
> >
> > I thought of
> >
> > t = A
> >
> > case t.new
> > when A then ...
> >
> >
> > But in my case, A and B actually have initialize methods, and they
> > require args (different args).
> >
> >
> > Thanks,
> > Sam
> >
> > --
> > Sam Roberts <sroberts@certicom.com>
>
>

--
Jamis Buck
jamis_buck@byu.edu
http://jamis.jam...
------------------------------
"I am Victor of Borge. You will be assimil-nine-ed."



Martin DeMello

2/23/2005 5:43:00 PM

0

Jason Bailey <azrael@demonlords.net> wrote:
> I'm thinking you can get what you want by
>
> case t
> when A.class then...
> when B.class then...
> end
>
> since you want to compare the actual class of the object

Or the inverse kludge:

case t.new
when A ...
when B ...
end