This is an object preinitialize system, which provides an elegant way to initialize an object allowing the class to provide additional default structure to an object prior to calling initialize.

In effect it does two things after allocating the object but prior to initializing it.

First, it calls the class method default_instance_variables, which returns a hash and by default returns the hash stored in @@default_instance_variables. It uses this to pre-define instance variables.

Then it goes to the top of the class hierarchy and works it’s way down calling preinitialize if defined. WARNING! It is rather useless to use super inside the any preinitialize hook.

  class X
    default_instance_values[:a] = 42

    def preinitialize
      @b = 23
    end
  end

  x = X.new
  x.a         #=> 42
  x.b         #=> 23

If neded the original new method has been aliased, albeit postinitialize_new is probably a bit of a misnomer.

Methods
Included Modules
Public Instance methods
default_instance_variables() [ source ]
new(*args,&blk) [ source ]