| [ next ] [ prev ] [ contents ] | XP-Cinti TDD Workshop |
Object creation is Ruby is straightforward, but slightly different than Java's way of doing things.
First of all, classes are full objects, responding to messages in the runtime system. When Ruby sees a class Net line, it creates class object and assigns it to a constant named Net.
One of the most important messages a class object responds to is the new message. When a class object receives new, it allocates a new object of its type and invokes the initialize method on that object. Any arguments passed to new will automatically be passed to initialize as well.
This is why we use the Net.new syntax when creating a new object.
That's an important distinction, new is a class method (sent to a class object) and initialize is an instance object (sent to an instance of a class).
Since the first method an object handles will be initialize, this is
an excellent time to do any object initialization.
| [ next ] [ prev ] [ contents ] | Copyright 2003 by Jim Weirich.![]() |