Learning how best to use instance variables
Almost all objects in Ruby support instance variables. As mentioned in Chapter 1, Getting the Most out of Core Classes, the exceptions are the immediate objects: true
, false
, nil
, integer, floats, and symbols. The reason the immediate objects do not support instance variables is that they lack their own identity. Ruby is written in C, and internally to Ruby, all Ruby objects are stored using the VALUE
type. VALUE
usually operates as a pointer to another, larger location in memory (called the Ruby heap). In that larger location in memory is where instance variables are stored directly, or if that isn't large enough, a pointer to a separate location in memory where they are stored.
Immediate objects are different from all other objects in that they are not pointers, they contain all information about the object in a single location in memory that is the same size as a pointer. This means there is no space for them to contain instance...