I want to create a hash but I’m only interested in the keys. As a consequence, I want the values to have the smallest memory footprint possible. What would be the most suitable object to assign?
nil?- a very short symbol like
:a? - Something even smaller?
The following applies to the official Ruby implementation. Other implementations may differ in this regard.
nil,true,falseandFixnums are encoded inside the pointer at the C level, whereas all other objects would involve a pointer that actually points somewhere (so you’d have the space consumption of the pointer plus the space it points to). So these objects are the ones with the smallest memory footprint.Of these,
nilmakes the most sense semantically.