Search code examples
rubyunsigned-integer

How can Floats and Symbols have immediate value?


I was informed by a fellow StackOverflow user that Floats now have immediate value in Ruby. However, I am confused as to how this is implemented.

I am also confused as to how Symbols can have immediate value.

I understand that objects with immediate value are objects who's entire state information can be encapsulated into an unsigned long C variable called VALUE.

I can intuitively understand how this would be possible when considering small integers(Fixnums), and trivial things like true false nil etc.

But, with no length restriction on Floats and Symbols, how can these objects be represented without their own structs?


Solution

  • First, a Float does a length restriction - a Float is basically a native double precision floating point value, i.e. 64 bits. That's still too much though, so a Float is an immediate value only if the mantissa is not too big (you can see the exact condition here).

    As for symbols, there is a data structure enumerating all created symbols, so they can be referred to as offsets into that table (on ruby 2.2 only some symbols are like this - those that aren't are "normal", garbage collectible objects).