Ruby Constructors: new or initialize?

I gave a brief explanation of new and initialize in Chapter 1. At that stage, you had not examined the differences between Ruby’s class and instance methods and variables, so it was not possible to give a full discussion of how new and initialize work together. Because these are such important methods, you’ll look at them in more detail now.

The method responsible for bringing an object into being is called the constructor. In Ruby, the constructor method is called new. The new method is a class method that, once it has created an object, will run an instance method named initialize if such a method exists.

In brief then, the new method is the constructor, and the initialize method is used to initialize the values of any variables immediately after an object is created. But why can’t you just write your own new method and initialize variables in it? Well, let’s try that:

new.rb

class MyClass
   def initialize( aStr )
      @avar = aStr
   end

   def MyClass.new( aStr )
      super
      @anewvar = aStr.swapcase
   end
end

ob = MyClass.new( "hello world" )
puts( ob )
puts( ob.class )

Here, I’ve written a MyClass.new method that begins with the super keyword to invoke the new method of its superclass. Then I’ve created a string instance variable, @anewvar. So what do I end up with? Not, as you might suppose, a new MyClass object containing a string variable. Remember that the last expression evaluated by a method in Ruby is the value returned by that method. The last expression evaluated by the new method here is a string. I evaluate this:

ob = MyClass.new( "hello world" )

And I display the newly created ob object and its class:

puts( ob )
puts( ob.class )

This is the output:

HELLO WORLD
String

This proves that MyClass.new returns a string, and it is this string (not a MyClass object) that is assigned to the variable ob. If you find this confusing, don’t panic. The moral of the story is that overriding new is confusing and is generally a bad idea. Unless you have a very good reason for doing so, you should avoid trying to override the new method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.248.162