Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you trace through the 1.9.3 source, you'll find that <code>defined?</code> is implemented in <a href="https://github.com/ruby/ruby/blob/ruby_1_9_3/insns.def#L749" rel="nofollow"><code>insns.def</code></a>:</p> <pre><code>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; </code></pre> <p>So when you <code>defined? SomeConstant</code>, you trickle through that big <code>switch</code> and end up calling <code>vm_get_ev_const</code>. The function is defined in <a href="https://github.com/ruby/ruby/blob/ruby_1_9_3/vm_insnhelper.c#L1160" rel="nofollow"><code>vm_insnhelper.c</code></a>:</p> <pre><code>static inline VALUE vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq, VALUE orig_klass, ID id, int is_defined) </code></pre> <p>That function happens to be static so you can't get at it. Looks like <code>vm_get_ev_const</code> is defined in terms of <a href="https://github.com/ruby/ruby/blob/ruby_1_9_3/variable.c#L1846" rel="nofollow"><code>rb_const_defined</code></a> and <a href="https://github.com/ruby/ruby/blob/ruby_1_9_3/variable.c#L1858" rel="nofollow"><code>rb_const_defined_from</code></a> and both of those should be available in your C so you could try those; but you'd have to find the right <code>klass</code> for those.</p> <p>Or you could go with your idea and just use <code>Object.const_defined?</code>. One problem with that is that it won't do The Right Thing with things like <code>A::B</code>, you'd have to say <code>Object.const_defined? :A &amp;&amp; A.const_defined? :B</code> for that as <code>Object.const_defined? :'A::B'</code> will just throw an exception in your face. A general solution here would require iteration and class lookups. However, if the classes that you're looking at are all in the top level namespace, then a simple <code>Object.const_defined?</code> should do the trick.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload