[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: what exactly does obj.is_a? do?

Vincent Fourmond

12/19/2006 7:48:00 PM

Jon Garvin wrote:
> I've got a theory that I hope someone familiar with the deep recesses of
> Ruby's internals can confirm or deny.
>
> According to the API, obj.is_a? "Returns true if /class/ is the
> class of /obj/, or if /class/ is one of the superclasses of /obj/ or
> modules included in /obj/."
> For the purpose of this question, the only part I'm concerned with is
> "Returns true if /class/ is the class of /obj/", so let's ignore the rest.//
>
> My theory is that Ruby is simply returning the result of...
>
> @my_obj.class.object_id == Klass.object_id
>
> when I do...
>
> @my_obj.is_a?(Klass)

No: is_a? also returns true when the @my_obj is an instance of a
subclass of Klass.

> (Don't forget we're ignoring the superclass and module stuff it does).
>
> Can anyone confirm or deny this?
>
> I'd also appreciate a hint pointing me to where I could have dredged up
> the answer to this one on my own. Thanks, and happy holidays!

If you do

ri is_a?

on command-line, you'll get immediate answer.

Vince

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

2 Answers

Jon Garvin

12/19/2006 8:00:00 PM

0

Vincent Fourmond wrote:
> No: is_a? also returns true when the @my_obj is an instance of a
> subclass of Klass.
>
>
I realize that, but that's beyond the scope of the issue I'm having.
That's why my original post explicitly said to ignore the superclass and
module stuff it does (twice).
> If you do
>
> ri is_a?
>
> on command-line, you'll get immediate answer.
>
>
Nope, all I get is the exact same API stuff I've already looked at
before asking, and quoted in my original post.

To clarify, I'm interested in *how*, not *if*, is_a? determines whether
@my_obj.is_a?(Klass) with no concern for superclasses or modules. I'm
fairly confident it's going to take somebody familiar with Ruby's source
code, not just the API, to answer this one.

Tim Pease

12/19/2006 9:15:00 PM

0

On 12/19/06, Jon Garvin <jgarvin.lists@gmail.com> wrote:
> Vincent Fourmond wrote:
> > No: is_a? also returns true when the @my_obj is an instance of a
> > subclass of Klass.
> >
> >
> I realize that, but that's beyond the scope of the issue I'm having.
> That's why my original post explicitly said to ignore the superclass and
> module stuff it does (twice).
> > If you do
> >
> > ri is_a?
> >
> > on command-line, you'll get immediate answer.
> >
> >
> Nope, all I get is the exact same API stuff I've already looked at
> before asking, and quoted in my original post.
>
> To clarify, I'm interested in *how*, not *if*, is_a? determines whether
> @my_obj.is_a?(Klass) with no concern for superclasses or modules. I'm
> fairly confident it's going to take somebody familiar with Ruby's source
> code, not just the API, to answer this one.
>

Have you downloaded the ruby source code and poked around a little
bit? It's always very educational -- Matz writes very readable C
code.

I would recommend grabbing the source code, open up "object.c" and do
a search for "rb_obj_is_kind_of". Answer should be right there. It's
not quite as simple as just comparing object IDs, but it is something
close to that.

Blessings,
TwP

PS I don't know exactly how much you want to know about the internals
of the ruby C code. A single ruby-talk posting would hardly do it
justice. Once resource to get you started, though, is the English
translation of the Ruby Hacking Guide:

http://rhg.ruby...