Ruby C API `defined? SomeConstant equivalent?

I am trying to convert an if condition:

unless defined? SomeConstant
  # do some stuff
end

As part of the internal extension C. Does anyone know how to do predicate checking defined?in the C API?

EDIT | I think I could call:

rb_funcall(rb_cObject, rb_intern("const_defined?"), 1, rb_intern("SomeConstant"))

Although this is clearly a bit different, semantically.

+3
source share
1 answer

If you trace through source 1.9.3, you will find what is defined?implemented in insns.def:

DEFINE_INSN
defined
(rb_num_t op_type, VALUE obj, VALUE needstr)
/* ... */
    switch (type) {
    /* ... */
      case DEFINED_CONST:
        klass = v;
        if (vm_get_ev_const(th, GET_ISEQ(), klass, SYM2ID(obj), 1)) {
            expr_type = "constant";
        }
        break;

So, when you defined? SomeConstant, you seep through this big one switchand end up calling vm_get_ev_const. The function is defined in vm_insnhelper.c:

static inline VALUE
vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq,
                VALUE orig_klass, ID id, int is_defined)

, . , vm_get_ev_const rb_const_defined rb_const_defined_from, C, ; klass.

Object.const_defined?. , " " , A::B, Object.const_defined? :A && A.const_defined? :B, Object.const_defined? :'A::B' . . , , , , Object.const_defined? .

+3

All Articles