Class#new
is just a normal method like any other method. It looks a bit like this, although in most implementations it is actually not written in Ruby:
class Class
def new(*args, &block)
new_obj = allocate
new_obj.initialize(*args, &block)
# actually, `initialize` is private, so it's more like this instead:
# new_obj.__send__(:initialize, *args, &block)
return new_obj
end
end
The documentation also says it clearly:
new(args, …)
→ obj
Calls allocate
to create a new object of class’s class, then invokes that object’s initialize
method, passing it args. This is the method that ends up getting called whenever an object is constructed using .new
.
Here's the source code for Class#new
in the various implementations: