logo       

Re: class === class often false?: msg#01160

lang.ruby.general

Subject: Re: class === class often false?

Hi --

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

> I'll take a stab at this one.
>
> If I'm wrong at any point, someone please correct me.

Likewise, and likewise :-)

> The === operator (method) may mean different things
> depending on what class it belongs to. That's no
> big news.
>
> When the receiver and the other operand (x === y)
> are both classes, it is (I think) essentially the
> same as saying:
>
> x.is_a? y

It's actually y.is_a? x

irb(main):003:0> String === Object
false
irb(main):004:0> Object === String
true

which I think is to help with case statement syntax:

case String
when Object...
etc.

> So far, so good.
>
> Note also that if a class includes a module, it
> then has the "is a" relationship with that module,
> and an included module shows up in the list of
> ancestors. (Since a module is a kind of parent --
> it's like a limited form of inheritance.)

It is among the ancestors, but there's no is_a? or === relationship:

irb(main):019:0> Array.ancestors
[Array, Enumerable, Object, Kernel]
irb(main):020:0> Array.is_a? Enumerable
false
irb(main):021:0> Enumerable === Array
false

However, Array objects do return true on these tests:

irb(main):026:0> [].is_a? Enumerable
true
irb(main):027:0> Enumerable === []
true

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.

Now, this one:

irb(main):015:0> Kernel === Object
true

is a little puzzling to me, because I would have thought Object:Kernel
was the same relationship as Array:Enumerable. Except... there is
that circularity/singularity at the top of the chain -- which I
alternate between thinking I understand and losing sleep over :-) In
this case I can't say I entirely understand exactly how/why it would
account for the result, but I think somehow it does.


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