static public Method

ObjectSpace._id2ref(p1)

ObjectSpace._id2ref(object_id)  an_object

Converts an object id to a reference to the object. May not be called on an object id passed as a parameter to a finalizer.

s = "I am a string"                    #=> "I am a string"
r = ObjectSpace._id2ref(s.object_id)   #=> "I am a string"
r == s                                 #=> true

Source Code

/*
*  call-seq:
*     ObjectSpace._id2ref(object_id) -> an_object
*
*  Converts an object id to a reference to the object. May not be
*  called on an object id passed as a parameter to a finalizer.
*
*     s = "I am a string"                    #=> "I am a string"
*     r = ObjectSpace._id2ref(s.object_id)   #=> "I am a string"
*     r == s                                 #=> true
*
*/

static VALUE
id2ref(obj, objid)
   VALUE obj, objid;
{
   unsigned long ptr, p0;

   rb_secure(4);
   p0 = ptr = NUM2ULONG(objid);
   if (ptr == Qtrue) return Qtrue;
   if (ptr == Qfalse) return Qfalse;
   if (ptr == Qnil) return Qnil;
   if (FIXNUM_P(ptr)) return (VALUE)ptr;
   ptr = objid ^ FIXNUM_FLAG;  /* unset FIXNUM_FLAG */

   if ((ptr % sizeof(RVALUE)) == (4 << 2)) {
       ID symid = ptr / sizeof(RVALUE);
       if (rb_id2name(symid) == 0)
           rb_raise(rb_eRangeError, "%p is not symbol id value", p0);
       return ID2SYM(symid);
   }

   if (!is_pointer_to_heap((void *)ptr)|| BUILTIN_TYPE(ptr) >= T_BLKTAG) {
       rb_raise(rb_eRangeError, "0x%lx is not id value", p0);
   }
   if (BUILTIN_TYPE(ptr) == 0 || RBASIC(ptr)->klass == 0) {
       rb_raise(rb_eRangeError, "0x%lx is recycled object", p0);
   }
   return (VALUE)ptr;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.