Typechecking in C extensions

E

Eric Hofreiter

--0-844079446-1131789087=:95667
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

I'm doing some experimenting with writing C extensions, and so far it is =
fairly simple and intuitive. However, there is one thing that I cannot f=
igure out. In ruby.h there are the native types define as T_STRING, T_FI=
XNUM, etc., allowing you to take an argument and see if it is a string or=
fixnum and act accordingly. How would I go about testing if the argumen=
t is a user-defined type? Is this even possible? Thanks in advance.

=09
 
T

Timothy Hunter

Eric said:
I'm doing some experimenting with writing C extensions, and so far it is fairly simple and intuitive. However, there is one thing that I cannot figure out. In ruby.h there are the native types define as T_STRING, T_FIXNUM, etc., allowing you to take an argument and see if it is a string or fixnum and act accordingly. How would I go about testing if the argument is a user-defined type? Is this even possible? Thanks in advance.

CLASS_OF(obj) == my_class

where 'my_class' is a class object VALUE such as that returned by
rb_define_class or rb_const_get (if the class is defined in Ruby).
 
G

Gyoung-Yoon Noh

s fairly simple and intuitive. However, there is one thing that I cannot f=
igure out. In ruby.h there are the native types define as T_STRING, T_FIXN=
UM, etc., allowing you to take an argument and see if it is a string or fix=
num and act accordingly. How would I go about testing if the argument is a=
user-defined type? Is this even possible? Thanks in advance.
CLASS_OF(obj) =3D=3D my_class

where 'my_class' is a class object VALUE such as that returned by
rb_define_class or rb_const_get (if the class is defined in Ruby).

I investigated a little more. I found it In ruby.h:

#define CLASS_OF(v) rb_class_of((VALUE)(v))

static inline VALUE
rb_class_of(VALUE obj)
{
if (IMMEDIATE_P(obj)) {
if (FIXNUM_P(obj)) return rb_cFixnum;
if (obj =3D=3D Qtrue) return rb_cTrueClass;
if (SYMBOL_P(obj)) return rb_cSymbol;
}
else if (!RTEST(obj)) {
if (obj =3D=3D Qnil) return rb_cNilClass;
if (obj =3D=3D Qfalse) return rb_cFalseClass;
}
return RBASIC(obj)->klass;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top