logo       

Re: class === class often false: msg#01180

lang.ruby.general

Subject: Re: class === class often false

Hi --

On Tue, 13 Aug 2002, Hal E. Fulton wrote:

> > A (whimsical) further test:
> >
> > irb(main):036:0> class Class; include Enumerable; end
> > Class
> > irb(main):037:0> Enumerable === Array
> > true
> >
> > So ancestors/is_a?/Module#=== are related but can vary independently.
>
> Yes, this is where my brain itches again.

I feel some culpability, given what a cop-out that concluding sentence
is :-) But we're in this together.... I definitely want to clear my
own mental cobwebs in this area.

OK, after some further tinkering, here's what I think:

1. "Mod === obj" and "obj.is_a? Mod" are in effect the same test.

2. Module#ancestors can be confusing because it's not
Object#ancestors. I mean, it's right that it isn't, but one finds
oneself expecting "abc".ancestors to be String, Enumerable.... (At
least, one does if one is me, or one is I, or one == I, or I === one.)
But only modules and classes have ancestors, and all ancestors are
modules or classes. This is a case where, even though they are
regular objects, modules and classes get to be different. (I repeat:
it makes complete sense that there's no Object#ancestors. The problem
is getting myself to grasp it.)

3. However, every object does respond to #type. In every case --
whether the RHS is a module or not -- "Mod === obj" tests whether
obj's type is among Mod's ancestors.

4. All of this means that this:

Enumerable === String

which perhaps looks like it's asking whether strings are Enumerable,
is in fact testing whether the object String is of a type which counts
Enumerable among its ancestors. Class (String's type) does not do
that, so this test is false.

Jotting down a possibly helpful train of thought:

Are Array objects Enumerable? Let's test one:

Enumerable === [] # true

Are Class objects Enumerable? Let's test one:

Enumerable === String # false

I'll leave the Kernel/Object/Module/Class circularity for another time
:-)


David

--
David Alan Black
home: dblack@xxxxxxxxxxxxxxxxxxxx
work: blackdav@xxxxxxx
Web: http://pirate.shu.edu/~blackdav




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise