Oren Shani wrote in post #1041899:
> This is how I do it and it works fine. Notice that I do not do the
> registration while initialising the object but with another method. This
> is because (as far as I understand) while the initialise method is
> executed by the object generator of the class, the object was not
> actually created yet.

No: the object *is* already created (allocated). The initialize method 
is there for you to set it up as desired, but 'self' is a completely 
formed object and it can call its own methods during initialize, and/or 
pass itself to other objects.

Whether this is a good design practice is another issue; it may make 
your objects less reusable if they are forcibly linked to a single 
container object.

Another option is to use a method of the container to create and add the 
objects: this lets you create free objects, and/or objects in distinct 
containers.

class Container
  def initialize
    @collection = {}
  end

  def create_foo(id, *args)
    @collection[id] = Foo.new(*args)
  end
end

-- 
Posted via http://www.ruby-forum.com/.