Vida de un Objeto
Objects of most
classes need to be explicitly created, and this is most often done
with a method named new:
myObject = myClass.new
new is a method of the Class class.
- It allocates memory to hold the
new object, then it initializes the state of that newly allocated
empty object by invoking its initialize method.
- The arguments to
new are passed directly on to initialize. Most classes define an
initialize method to perform whatever initialization is necessary
for instances.
- The new and initialize methods provide the default technique for
creating new objects, but classes may also define other methods,
known as factory methods that return instances.
- Ruby objects never need to be explicitly deallocated, as they do
in languages like C and C++. Ruby uses a technique called
garbage collection to automatically destroy objects that are no longer
needed.
- An object becomes a candidate for garbage collection when
it is unreachable: when there are no remaining references to the
object except from other unreachable objects.
- Garbage collection does not mean that memory leaks are impossible:
any code that creates long-lived references to objects that would
otherwise be short-lived can be a
source of memory leaks.
Casiano Rodriguez León
2015-01-07