0

Module in macruby has many methods that it doesn't usually have. One of these is __type__ and I simply can't seem to figure out what it does. What does it do?

Thanks!

z.

Brett Bender
  • 19,388
  • 2
  • 38
  • 46
Ziggy
  • 21,845
  • 28
  • 75
  • 104

1 Answers1

1

__type__ is defined in object.c as:

static VALUE
rb_obj_type(VALUE obj)
{
    return LONG2FIX(TYPE(obj));
}

which in turns depends on the rb_type function:

static inline int
rb_type(VALUE obj)
{
    if (IMMEDIATE_P(obj)) {
    if (FIXNUM_P(obj)) {
        return T_FIXNUM;
    }
    if (FIXFLOAT_P(obj)) {
        return T_FLOAT;
    }
    if (obj == Qtrue) {
        return T_TRUE;
    }
    if (obj == Qundef) {
        return T_UNDEF;
    }
    }
    else if (!RTEST(obj)) {
    if (obj == Qnil) {
        return T_NIL;
    }
    if (obj == Qfalse) {
        return T_FALSE;
    }
    }
    return rb_objc_type(obj);
}

Definitely, it simply returns a number that corresponds to a type identifier, as defined by the pre compiler constants T_FIXNUM, T_FLOAT, etc.

I would say that it is something of really limited use for standard users, although it may make your code more efficient in type checking when you write C extensions.

p4010
  • 943
  • 7
  • 19