APIdock / Ruby
/
method

object_id

ruby latest stable - Class: Object
object_id()
public

Returns an integer identifier for obj.

The same number will be returned on all calls to object_id for a given object, and no two active objects will share an id.

Note: that some objects of builtin classes are reused for optimization. This is the case for immediate values and frozen string literals.

Immediate values are not passed by reference but are passed by value: nil, true, false, Fixnums, Symbols, and some Floats.

Object .new .object_id  == Object .new .object_id  # => false
(21 * 2).object_id  == (21 * 2).object_id  # => true
"hello".object_id  == "hello".object_id  # => false
"hi".freeze .object_id  == "hi".freeze .object_id  # => true
VALUE
rb_obj_id(VALUE obj)
{
 /*
 * 32-bit VALUE space
 * MSB ------------------------ LSB
 * false 00000000000000000000000000000000
 * true 00000000000000000000000000000010
 * nil 00000000000000000000000000000100
 * undef 00000000000000000000000000000110
 * symbol ssssssssssssssssssssssss00001110
 * object oooooooooooooooooooooooooooooo00 = 0 (mod sizeof(RVALUE))
 * fixnum fffffffffffffffffffffffffffffff1
 *
 * object_id space
 * LSB
 * false 00000000000000000000000000000000
 * true 00000000000000000000000000000010
 * nil 00000000000000000000000000000100
 * undef 00000000000000000000000000000110
 * symbol 000SSSSSSSSSSSSSSSSSSSSSSSSSSS0 S...S % A = 4 (S...S = s...s * A + 4)
 * object oooooooooooooooooooooooooooooo0 o...o % A = 0
 * fixnum fffffffffffffffffffffffffffffff1 bignum if required
 *
 * where A = sizeof(RVALUE)/4
 *
 * sizeof(RVALUE) is
 * 20 if 32-bit, double is 4-byte aligned
 * 24 if 32-bit, double is 8-byte aligned
 * 40 if 64-bit
 */
 if (STATIC_SYM_P(obj)) {
 return (SYM2ID(obj) * sizeof(RVALUE) + (4 << 2)) | FIXNUM_FLAG;
 }
 else if (FLONUM_P(obj)) {
#if SIZEOF_LONG == SIZEOF_VOIDP
 return LONG2NUM((SIGNED_VALUE)obj);
#else
 return LL2NUM((SIGNED_VALUE)obj);
#endif
 }
 else if (SPECIAL_CONST_P(obj)) {
 return LONG2NUM((SIGNED_VALUE)obj);
 }
 return nonspecial_obj_id(obj);
}

AltStyle によって変換されたページ (->オリジナル) /