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.

  1. It allocates memory to hold the new object, then it initializes the state of that newly allocated empty object by invoking its initialize method.

  2. The arguments to new are passed directly on to initialize. Most classes define an initialize method to perform whatever initialization is necessary for instances.

  3. 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.

  4. 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.

  5. 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.

  6. 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