# Re: what exactly does obj.is\_a? do?

**URL:** <https://www.ruby-forum.com/t/re-what-exactly-does-obj-is-a-do/79921>\
**Category:** Ruby\
**Created:** [December 20, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/re-what-exactly-does-obj-is-a-do/79921 "2006-12-20T18:21:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 20, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/re-what-exactly-does-obj-is-a-do/79921/1 "2006-12-20T18:21:40Z")

</div>

From: Jon G. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> 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.

The source code is readily available. Here, from object.c, is the answer  
to your question. (Out of curiosity, why are you so curious as to the  
internals?)

VALUE  
rb\_obj\_is\_kind\_of(obj, c)  
VALUE obj, c;  
{  
VALUE cl = CLASS\_OF(obj);

```
switch (TYPE(c)) {
  case T_MODULE:
  case T_CLASS:
  case T_ICLASS:
break;

  default:
rb_raise(rb_eTypeError, "class or module required");
}

while (cl) {
if (cl == c || RCLASS(cl)->m_tbl == RCLASS(c)->m_tbl)
    return Qtrue;
cl = RCLASS(cl)->super;
}
return Qfalse;

```

}

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [December 20, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/re-what-exactly-does-obj-is-a-do/79921/2 "2006-12-20T19:16:34Z")

</div>

Gavin K. wrote:

> The source code is readily available. Here, from object.c, is the answer  
> to your question. (Out of curiosity, why are you so curious as to the  
> internals?)

Thanks Gavin. Unfortunately, my C skills are very rusty. It seems to  
boil down to this line…

if (cl == c || RCLASS(cl)-\>m\_tbl == RCLASS©-\>m\_tbl)

but it’s not clear to me if object\_id’s are critical here or not. Would  
it be possible for cl and c to be different objects, and still be  
evaluated by this condition as equal?

To satisfy your curiosity, this was due to an issue we were having with  
comparing a cached object to an apparently reloaded class and finding  
that the object was not of that class when we thought it should be.  
Since my original post we’ve identified the issue/solution with our  
caching routine, so at this point this thread is pretty much just about  
knowledge for it’s own sake.
