Instantiation and the Initialize Method

of 02

Instantiation and the Initialize Method

Learning Ruby on Rails: Playing with irb
brnzwngs/Flikr/CC BY 2.0

When you define a class in Ruby, Ruby will assign a new class object to the class name constant. For example, if you were to say class Person; end, this is roughly equivalent to Person = This class object is of the type Class, and holds a number of methods useful for making instances of copies of those instances.

of 02

Making Instances

To make a new instance of a class, call that class's new method. By default, this will allocate the required memory for the class and return a reference to the new object. So, if you were to make a new instance of the Person class, you would call

While at first this seems a bit backwards, there is no new keyword in Ruby or any special syntax. New objects are created through a normal method that, all said and done, does relatively simple things.

Initializing Instances

A blank object is not very exciting. In order to start using your object, it must first be initialized (assuming it has any instance variables that needs initializing). This is done via the initialize method. Ruby will pass any arguments you pass to on to initialize on the new object. You can then use normal variable assignments and methods to initialize the state of the object. In this example, a Person class is presented whose initialize method will take a name and age argument, and assign them to instance variables.

class Person def initialize(name, age) @name, @age = name, age end end bob ='Bob', 34)

You can also use this opportunity to acquire any resources you may need. Open network sockets, open files, read in any data you need, etc. The only caveat is that people generally don't expect initialize methods to fail. Be sure to document any possibly failing initialize methods thoroughly.

Destorying Objects

In general, you don't destroy objects in Ruby. If you're coming from C++ or another language without a garbage collector, this might seem strange. But in Ruby (and most other garbage collected languages), you don't destroy objects, you simply stop referring to it. On the next garbage collection cycle, any object without anything referring to it will be destroyed automatically. There are some bugs with circular references, but in general this works flawlessly and you don't even need a "destructor."

If you're wondering about resources, don't worry about it. When the object holding the resource is destroyed, the resource will be freed. Open files and network connections will be closed, memory deallocated etc. Only if you allocate any resources in a C extension will you really need to worry about deallocating resources. Though there is no guarantee when the garbage collector will be run. In order to deallocate resources in a timely manner, try to free them manually.

Making Copies of Objects

Ruby is pass by reference. If you pass a reference to an object to a method, and that method calls a method that modifies the state of that object, unintended consequences can occur. Further, methods can then save the reference to the object to modify at a much later time, causing a delayed effect for the bug. To avoid this, Ruby provides some methods to duplicate objects.

To duplicate any object, simply call the some_object.dup method. A new object will be allocated and all of the object's instance variables will be copied over. However, copying instance variables is what this was supposed to avoid: this is what's called a "shallow copy." If you were to hold a file in an instance variable, both of the duplicated objects would now be referring to the same file.

Just be aware that the copies are shallow copies before using the dup method. See the article Making Deep Copies in Ruby for more information.