4 ms·
Ruby does something similar, but all Fixnum (native sized) values are 'fixed objects': a = 2**62 - 1 b = 2**62 - 1 a.object_id == b.object_id # tr
by mbell 9y ago
Ruby does something similar, but all Fixnum (native sized) values are 'fixed objects':
a = 2**62 - 1
b = 2**62 - 1
a.object_id == b.object_id # true
a = 2**62
b = 2**62
a.object_id == b.object_id # false
Ruby does automatic promotion from Fixnum (native size) to Bignum (arbitrarily large) and uses one bit of the native size as a flag to identify this which is why 2^62 - 1 is the max instead of 2^63 - 1. Though I think this is only true of MRI and other implementations handle it without the flag bit.
Perhaps one difference from Python is that in MRI Ruby Fixnum doesn't really even allocate an 'object', the object_id is the value in disguise. In fact all 'real' objects have even object_ids and all odd object_ids are integers:
a = 123456789
(a.object_id - 1) / 2 # 123456789