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.
The partition method would need to return an array of Array
Sublcasses.
I know I can make an Array subclass (or another class altogether)
that itself can be subclassed and would do what I wanted. I just
wondered if there is a much easier general solution that I am
missing.
example:
class Q
attr_accessor :e, :r
def initialize(e,r)
@e,@r = e,r
end
end
class QArray < Array
def sum_e
inject(0){|sum, item| sum+=item.e}
end
end
qArray = QArray.new
qArray << Q.new(49, 'A')
qArray << Q.new(50, 'B')
qArray << Q.new(51, 'A')
puts qArray.class
puts qArray.sum_e
puts qArray.find_all{|item| item.e < 50}.sum_e #fails here
Can find_all defined in Array be coerced into returning
a QArray and not a plain Array?
Thanks,
Walt
*****************************************************
Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter / mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284
*****************************************************