From: EbFFA Date: 2009-08-25T15:55:09+09:00 Subject: Re: doubling numbers with Object#object_id Aah that explains everything, thankyou. That's a very beautiful system. Makes me want to design my own interpreter and take part in such things. On Tue, Aug 25, 2009 at 12:26 AM, Nilesh Trivedi wrote: > All other Ruby objects are allocated on addresses that are multiples > of 4 to avoid clash with FixNum. So Object ID is always even for them. > > cheers > nilesh > > On Tue, Aug 25, 2009 at 11:54 AM, Nilesh Trivedi wrote: >> AFAIK, FixNums are implemented as direct values and not C structs that >> represent objects. Small integers - which means integers that can be >> held in (sizeof(unsigned long int) * 8 -1 = 31 bits). 1 bit is used >> for sign and 30 bits for the value. >> >> Here is the INT2FIX macro from Ruby source: >>  #define INT2FIX(i) ((VALUE)(((long)(i))<<1 | FIXNUM_FLAG)) >> >> Shift 1 bit to the left, and bitwise OR it with 1. In other words, >> object_id = 2n + 1. That explains why your double method works. >> >> cheers >> nilesh >> >> On Tue, Aug 25, 2009 at 11:36 AM, EbFFA wrote: >>> I was playing with Ruby and wrote an interesting method: >>> >>>    def double n >>>      n.object_id - 1 >>>    end >>> >>>    irb> double 5 >>>      => 10 >>>    irb> double 1 >>>      => 2 >>>    irb> double 0 >>>      => 0 >>>    irb> double -101 >>>      => -202 >>>    irb> double 100000001 >>>      => 200000002 >>>    irb> double 10000000000    # into Bignum territory now >>>      => -606548539 >>> >>> Anyone know why this works? Why Ruby was implemented this way? >>> >>> I tried creating an object_id collision based on this doubling rule, >>> but it seems that all Fixnum object_id`s are odd, and all other >>> object_id`s are even (in fact they all seem to end in 8). Clever. >>> >>> >> > >