Constructors
A look into how Ruby’s object construction works, and how we can customize constructors for special scenarios.
Script
If you came to Ruby from a language like Java, JavaScript, or C#, one of the differences you probably noticed early on was how Ruby handles creating objects. In most modern OO languages, creating an instance looks something like this, with the keyword new followed by the name of the class:
new Point(3,5);
Whereas in Ruby, the new is a method called on the class:
Point.new(3,5)
And then things get stranger still: whereas in most other languages, a constructor is a method with the same name as the class, in Ruby we define our constructor by writing a method called "initialize".
class Point
def initialize(x,y)
@x = x
@y = y
end
end
Today, let's explore how Ruby constructs new objects, and how we can customize the process to our own ends.
The easiest way to understand object construction in Ruby is to rewrite it ourselves.
We'll start with a class-level method called .my_new.
class Point
def self.my_new(x,y)
end
end
To construct a new instance, Ruby needs to do three things:
- Allocate a new, empty object of the given class.
- Run any specialized initialization code defined for the class; and
- Return the initialized instance.
For step one, allocating a new, empty object, we use a built-in method called, appropriately enough, allocate.
class Point
def self.my_new(x,y)
instance = allocate
end
end
Remember that we are defining a class method here, so the object we are sending #allocate to is the Point class itself.
Step two is running specialized initialization for the object. In this case, we want to set two instance variables, @x and @y. But setting instance variables from outside the instance is pretty painful:
class Point
def self.my_new(x,y)
instance = allocate
instance.instance_variable_set(:@x, x)
instance.instance_variable_set(:@x, y)
end
end
It would be easier if we could delegate that task to an instance method on the new object. We'll call the instance method #my_initialize.
class Point
def self.my_new(x,y)
instance = allocate
instance.my_initialize(x,y)
end
def my_initialize(x,y)
@x = x
@y = y
end
end
It occurs to us that the Point initializer might not always take just two arguments. What if we added names for the points? Or a third dimension? We don't want to have to change the list of parameters in three different places. So we generalize the constructor method to take any arguments, and simply pass them along to the initializer instance method as-is using the splat operator.
While we're at it, we also arrange for any block provided to the constructor to be passed along as well, using an & parameter.
class Point
def self.my_new(*args, &block)
instance = allocate
instance.my_initialize(*args, &block)
end
def my_initialize(x,y)
@x = x
@y = y
end
end
The third step in object construction is to return the initialized object. We update our constructor method to return the new instance.
class Point
def self.my_new(*args, &block)
instance = allocate
instance.my_initialize(*args, &block)
instance
end
# ...
end
And there we have it: a complete reimplementation of how Ruby's own .new method works. We can try it out and see that it works:
p = Point.my_new(3,5)
When we look at our implementation of .my_new, we can see that the call to #my_initialize functions as a kind of callback: it handles class-specific instance initialization only, while the class-level .my_new method takes care of allocating an object and making sure that the new object is returned after initialization.
The code we've just written is exactly how Ruby's own .new and #initialize work. And when we realize this, we realize we can make our own constructors to serve different purposes. For instance, consider a Point class which may be instantiated with either polar or Cartesian coordinates. Both coordinate systems use two numbers to locate a point. How can the initializer figure out which coordinate system its arguments belong to?
We can make the coordinate system explicit by defining specialized constructors for each:
class Point
def self.new_cartesian(x, y)
instance = allocate
instance.initialize_cartesian(x, y)
instance
end
def self.new_polar(distance, angle)
instance = allocate
instance.initialize_cartesian(polar_to_cartesian(distance, angle))
instance
end
# ...
end
Knowing that .new and #initialize are just ordinary methods means we can customize them in other ways. For instance, a common way to prevent a class from being instantiated in the usual way is to make the .new method private. Here's a class which uses this technique to ensure that there is only one instance of it.
class Snowflake
class << self
private :new
end
def self.instance
@instance ||= new
end
end
Snowflake.instance # => #<Snowflake:0x00000004af4db8>
Snowflake.instance # => #<Snowflake:0x00000004af4db8>
Snowflake.new # =>
# ~> -:14:in `<main>': private method `new' called for Snowflake:Class (NoMethodError)
This is also how Ruby's standard "Singleton" library works.
require 'singleton'
class TheOne
include Singleton
end
TheOne.instance # => #<TheOne:0x00000004a0b190>
TheOne.instance # => #<TheOne:0x00000004a0b190>
TheOne.new
# ~> -:8:in `<main>': private method `new' called for TheOne:Class (NoMethodError)
We can also do more exotic things, like keep a cache of instances and return objects from the cache instead of newly allocated instances. Here's a class which represents moves in the game rock-paper-scissors. Since there are only three valid moves, it caches each move using a hash keyed on the name of the move. As we can see from the identical object IDs, calling RpsMove.new more than once for the same move will return the same instance rather than creating a new one.
class RpsMove
def self.new(move)
@cache ||= {}
@cache[move] ||= super(move)
end
def initialize(move)
@move = move
end
end
RpsMove.new(:rock) # => #<RpsMove:0x00000004967428 @move=:rock>
RpsMove.new(:rock) # => #<RpsMove:0x00000004967428 @move=:rock>
Hopefully you now understand object construction in Ruby a little better. Happy hacking!
I used to think there was some sort of holy light surrounding
newandinitialize.Of course I’d worked with constructors in Javascript a bunch in the past. This screencast helped show me how, at the end of the day, Ruby serves the developer, not the other way around. Also, the customized home-made singleton class had me thinking about some ways I can improve memory performance……..
Thanks for the screencast!
You’re welcome!
I am really appreciate for you great video tutorials. I have a question, I will be glad if you reply me.
I am trying to complete the polar/Cartesian code, here’s what I have got:
class Point
def self.new_cartesian(x, y)
instance = allocate
instance.cartesian_initialize(x, y)
instance
end
def self.new_polar(distance, angle)
instance = allocate
instance.cartesian_initialize(polar_to_cartesian(distance, angle))
instance
end
def self.polar_to_cartesian(distance, angle)
# (r, θ) -> (r cosθ, r sinθ)
y = distance * Math.sin(angle)
x = distance * Math.cos(angle)
return x, y
end
def cartesian_initialize(x, y)
@x = x
@y = y
end
end
cartesian_point = Point.new_cartesian(3, 4)
polar_point = Point.new_polar(5, 0.7854)
p cartesian_point
p polar_point #=> 21:in `cartesian_initialize’: wrong number of arguments (given 1, expected 2) (ArgumentError)
is there a convenient way to solve this?
I hope you could solve in the meantime. For what I could see with your example, the problem is that in the line that you think you’re returning two values (
return x, y), you’re actually returning one value (an Array[x, y]). That means that in the line that you’re calling this method, you actually should be handling these two values.So, instead of
You should do
Hope that helps you (or any other reading this).
This video isn’t playing for 😭
Hm, I can’t reproduce. Can you tell me about your browser and any privacy/anonymizer plugins you’re using? A lot of times these issues turn out to be privacy plugins incorrectly interfering with the video embed.
Thanks all sorted, a thanks for an awesome site!
It is today, only vid I have had issues with
This is amazing.
Thank you!!