Hi --

On Thu, 18 Mar 2004 walter / mwsewall.com wrote:

> Is it possible to have the Superclass method return an instance of 
> the Subclass type.
> 
> Basically are there any hooks that would allow a Array methods return 
> an instance of a sub-class of Array.

This question has come up before in several contexts -- for instance,
if you subclass String, getting back an instance of MyString (or
whatever) when you do matches or substrings.  I believe the short
answer, based on what Matz has said in the past, is no :-) (at least
not automatically) 

One route you might want to take is to lightly wrap the methods whose
behavior you want to change:

  class QArray < Array
    def find_all
      QArray.new.replace(super)
    end
  end

and this kind of thing could be modularized and done in batches (e.g.,
you could have a SelfReturning module which wrapped a number of
Enumerable methods this way all at once for any class that included
it). 


David

-- 
David A. Black
dblack / wobblini.net