From: Mark Hubbart Date: 2004-03-18T03:06:37+09:00 Subject: Re: Can subclass have superclass method return instance of subclass On Mar 17, 2004, at 8:41 AM, walter@mwsewall.com wrote: > Can find_all defined in Array be coerced into returning > a QArray and not a plain Array? The simplest solution: add a to_qa method to your QArray class. Then call it like this: qArray.find_all{|item| item.e < 50}.to_qa.sum_e A slightly less-simple solution: Don't subclass Array, wrap it. Then, wrap the array methods so it coerces the returned value if an Array is returned. class QArray def initialize(ary) @ary = ary end def sum_e @ary.inject(0){|sum, item| sum+=item.e} end def method_missing(*args) if @ary.responds_to? args.first response = @ary.send(*args) if response.kind_of? Array QArray.new( response ) else response end else raise NoMethodError end end end I didn't test the code, but I'm pretty sure the logic is sound. --Mark