new, initialize and allocate
Here, person
is an instance of Person
.
We can’t do much with person
, so let’s add some concepts to it. A Person
has a name and an age. In the “Everything is an object” section we said that an object has a type and responds to some methods, which is the only way to interact with objects, so we’ll need both name
and age
methods. We will store this information in instance variables, which are always prefixed with an at (@
) character. We also want a Person to come into existence with a name of our choice and an age of zero. We code the “come into existence” part with a special method, which is normally called a constructor:
(If you wonder why we needed to specify that name
is a String
but we didn’t need to do it for age
, check the global type inference algorithm)
Note that we create a Person
with new
but we defined the initialization in an initialize
method, not in a new
method. Why is this so?
First, note the self.new
notation. This is a that belongs to the class Person
, not to particular instances of that class. This is why we can do Person.new
.
Second, allocate
is a low-level class method that creates an uninitialized object of the given type. It basically allocates the necessary memory for the object, then initialize
is invoked on it and finally the instance is returned. You generally never invoke allocate
, as it is unsafe, but that’s the reason why and initialize
are related.