Using ruby-hacking-guide site, I've found that fixnum << 8 | 1
is object_id of any fixnum.
I've tried using similar approach with symbol.
#define ID2SYM(x) ((VALUE)(((long)(x))<<8|SYMBOL_FLAG))
When shifting 8 bits left,
x
becomes a multiple of 256, that means a multiple of 4. Then after with a bitwise or (in this case it’s the same as adding) with0×0e
(14 in decimal)
I have tried it with :a
(:a.object_id
= 175_976, on my 32-bit system):
- ASCII number of a is 97.
- 97 << 8 = 24832
- 24832 | 14 = 24_846
So it's not even close to :a
's object id.
I've checked source of object_id
and found this:
* 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 (SYMBOL_P(obj)) {
return (SYM2ID(obj) * sizeof(RVALUE) + (4 << 2)) | FIXNUM_FLAG;
I got ~ 500 000, which is bad value.
So what I'm missing? How to calculate object_id of symbol?