[Nitro] Concerns over Og mandating the form of initialize

James Britt james_b at neurogami.com
Tue Apr 12 02:17:03 EDT 2005

George Moschovitis wrote:
> Hello James,
> the problem is not that serious (as you said you *can* use a
> constructor with arguments, they should just default to null).

But then your constructor has no way to ensure that the object i ever 
correctly initialized.  That's my concern.

The "yield self from initialize" version at least gives the option of 
coding some validity checks during initialization.

> However, I 'll try to find a better solution. Of course I 'll really
> appreciate a patch for this :)

Ah,  well, of course.

How abut something like this, for initializing objects:

def object_init( klass, vals = {} )
   obj = klass.new do |o|
     until  vals.empty?
       prop, val = *(vals.shift )
       o.send( "#{prop}=", val )

   return obj if vals.empty?

   vals.each do |prop, val|
     obj.send( "#{prop}=", val )

It first tries to populate an object that yields self from 'new'
If the block if never called, then the current Og approach is used.

This way, existing apps still work, and new apps have the option of 
using constructors that yield self.


More information about the Nitro-general mailing list