One of the things that makes Ruby great is that we can customize almost anything to our needs. This is both useful and dangerous. It's easy to shoot ourselves in the foot, but when used carefully, this can result in pretty powerful solutions.
At Ruby Magic, we think useful and dangerous is an excellent combination. Let's look at how Ruby creates and initializes objects and how we can modify the default behavior.
The Basics of Creating New Objects from Classes
To get started, let's see how to create objects in Ruby. To create a new object (or instance), we call new
on the class. Unlike other languages, new
isn't a keyword of the language itself, but a method that gets called just like any other.
In order to customize the newly created object, it is possible to pass arguments to the new
method. Whatever is passed as arguments, will get passed to the initializer.
Again, unlike other languages, the initializer in Ruby is also just a method instead of some special syntax or keyword.
With that in mind, shouldn't it be possible to mess around with those methods, just like it is possible with any other Ruby method? Of course it is!
Modifying the Behavior of a Single Object
Let's say we want to ensure that all objects of a particular class will always print log statements, even if the method is overridden in subclasses. One way to do this is to add a module to the object's singleton class.
In this example, a Bird
object is created using Bird.new
, and the Logging
module is included in the resulting object using its singleton class.
What's a Singleton Class?
Ruby allows methods that are unique to a single object. To support this, Ruby adds an anonymous class between the object and its actual class. When methods are called, the ones defined on the singleton class get precedence over the methods in the actual class. These singleton classes are unique to every object, so adding methods to them doesn't affect any other objects of the actual class.
Learn more about classes and objects in the Programming Ruby guide.
It's a bit cumbersome to modify the singleton class of each object whenever it is created. So let's move the inclusion of the Logging
class to the initializer to add it for every created object.
While this works well, if we create a subclass of Bird
, like Duck
, its initializer needs to call super
to retain the Logging
behavior. While one can argue that it's always a good idea to properly call super
whenever a method is overridden, let's try to find a way that doesn't require it.
If we don't call super
from the subclass, we lose the inclusion of the Logger
class:
Instead, Let's override Bird.new
. As mentioned before, new
is just a method implemented on classes. So we can override it, call super, and modify the newly created object to our needs.
But, what happens when we call make_noise
in the initializer? Unfortunately, because the singleton class doesn't include the Logging
module yet, we won't get the desired output.
Luckily, there's a solution: It's possible to create the default .new
behavior from scratch by calling allocate
.
Calling allocate
returns a new, uninitialized object of the class. So afterward, we can include the additional behavior and only then, call the initialize
method on that object. (Because initialize
is private by default, we have to resort to using send
for this).
The Truth About Class#allocate
Unlike other methods, it's not possible to override allocate
.
Ruby doesn't use the conventional way of dispatching methods for
allocate
internally. As a result, just overriding
allocate
without also overriding new
doesn't work.
However, if we're calling allocate
directly, Ruby will call the
redefined method.
Learn more about Class#new
and Class#allocate
in
Ruby's documentation.
Why Would We Do This?
As with a lot of things, modifying the way Ruby creates objects from classes can be dangerous and things might break in unexpected ways.
Nonetheless, there are valid use cases for changing the object creation. For instance, ActiveRecord uses allocate
with a different init_from_db
method to change the initialization process when creating objects from the database as opposed to building unsaved objects. It also uses allocate
to convert records between different single-table inheritance types with becomes
.
Most important, by playing around with object creation, you get a deeper insight into how it works in Ruby and open your mind to different solutions. We hope you enjoyed the article.
We'd love to hear about the things you implemented by changing Ruby's default way of creating objects. Please don't hesitate to tweet your thoughts to @AppSignal.