# How to judge the type of an object which is passed as an argument from Ruby to C?

**URL:** <https://www.ruby-forum.com/t/how-to-judge-the-type-of-an-object-which-is-passed-as-an-argument-from-ruby-to-c/127018>\
**Category:** Ruby\
**Created:** [January 24, 2008, 8:04am UTC](https://www.ruby-forum.com/t/how-to-judge-the-type-of-an-object-which-is-passed-as-an-argument-from-ruby-to-c/127018 "2008-01-24T08:04:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiwei\_zhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@shiwei\_zhang](https://www.ruby-forum.com/u/shiwei_zhang)\
**Post date:** [January 24, 2008, 8:04am UTC](https://www.ruby-forum.com/t/how-to-judge-the-type-of-an-object-which-is-passed-as-an-argument-from-ruby-to-c/127018/1 "2008-01-24T08:04:36Z")

</div>

Hi,

I am writing some programs hybrid of ruby and C. Rat, Buffalo, and Ranch  
are classes defined by me. Now in the Ruby program ranch.rb I pass an  
object (say, a Rat object rato) as an argument to the method  
“Ranch.new()”. Then Ranch.new(herder, role, rato) will invoke the  
function “ranch\_initialize()” in C program ranch.c. At this moment in  
the body of the function “ranch\_initialize()”, how should I judge/decide  
the variable vsubject is a Rat object, or a Buffalo object, or a  
String? The reason why I want to judge the type of the variable  
vsubject is: if it’s a Rat object I will do A operation, if it’s a  
Buffalo object I will do B operation, if it’s a String I will do C  
operation. It’s like the function overloading for the parameter  
vsubject. Please see part of the codes below.

_ranch.rb:_  
rato = Rat.new(user, pass)  
buffaloo = Buffalo.new(user, pass)  
planto = “plant string”  
rancho = Ranch.new(herder, role, rato)  
#rancho = Ranch.new(herder, role, buffaloo)  
#rancho = Ranch.new(herder, role, planto)  
…

_ranch.c:_  
static VALUE ranch\_initialize(int argc, VALUE \*argv, VALUE self)  
{  
VALUE vherder;  
VALUE vrole;  
VALUE vsubject;  
rb\_scan\_args(argc, argv, “21”, &vherder, &vrole, &vsubject);  
//Now how can I judge vsubject is a Rat object, or a Buffalo object,  
//or a String? Does the methods ‘if(TYPE(vsubject)==T\_OBJECT){…}’  
or  
//‘if(rb\_obj\_is\_instance\_of(vsubject,“Rat”)){…}’ work for this  
purpose?  
…  
}  
…  
cRanch = rb\_define\_class(“Ranch”, rb\_cObject);  
…  
rb\_define\_method(cRanch, “initialize”, ranch\_initialize, -1);  
…

Thank you very much!

Best Regards,  
Shiwei

---

<div class="post-metadata">

**Author:** ![shiwei\_zhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@shiwei\_zhang](https://www.ruby-forum.com/u/shiwei_zhang)\
**Post date:** [January 24, 2008, 8:20am UTC](https://www.ruby-forum.com/t/how-to-judge-the-type-of-an-object-which-is-passed-as-an-argument-from-ruby-to-c/127018/2 "2008-01-24T08:20:23Z")

</div>

shiwei zhang wrote:

> how should I judge/decide  
> the variable vsubject is a Rat object, or a Buffalo object, or a  
> String?

If you want to include inheritance:

rb\_obj\_is\_kind\_of(obj, some\_class)

If you do not, you can use the CLASS\_OF macro:

CLASS\_OF(obj) == some\_class
