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_ids are even (in fact they all seem to end in 8). Clever.