public Method

Object.kind_of?(p1)

obj.is_a?(class)        true or false
obj.kind_of?(class)     true or false

Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.

module M;    end
class A
  include M
end
class B < A; end
class C < B; end
b = B.new
b.instance_of? A   #=> false
b.instance_of? B   #=> true
b.instance_of? C   #=> false
b.instance_of? M   #=> false
b.kind_of? A       #=> true
b.kind_of? B       #=> true
b.kind_of? C       #=> false
b.kind_of? M       #=> true

Source Code

/*
*  call-seq:
*     obj.is_a?(class)       => true or false
*     obj.kind_of?(class)    => true or false
*  
*  Returns <code>true</code> if <i>class</i> is the class of
*  <i>obj</i>, or if <i>class</i> is one of the superclasses of
*  <i>obj</i> or modules included in <i>obj</i>.
*     
*     module M;    end
*     class A
*       include M
*     end
*     class B < A; end
*     class C < B; end
*     b = B.new
*     b.instance_of? A   #=> false
*     b.instance_of? B   #=> true
*     b.instance_of? C   #=> false
*     b.instance_of? M   #=> false
*     b.kind_of? A       #=> true
*     b.kind_of? B       #=> true
*     b.kind_of? C       #=> false
*     b.kind_of? M       #=> true
*/

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;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.