From: walter@... Date: 2004-03-18T02:35:15+09:00 Subject: Re: Can subclass have superclass method return instance of subclass > walter@mwsewall.com wrote: > > > class QArray < Array > > def sum_e > > inject(0){|sum, item| sum+=item.e} > > end > > end > > Why not just put sum_e into Array, instead of creating a subclass? > That would then allow it to be accessed from any array object. > > class Array > def sum_e > ... > end > end > > ... > sum = a.find_all {...}.sum_e > That would work in this simple example... What I find is that I tend to perform similar tasks on groups of Objects. We have classes that represent different objects in our domain ie Customers, invoices, deliveries, etc... and we tend to perform similar tasks on groups of these classes. Sum all invoices, find invoices that are overdue, find most recent delivery, etc. I have created CustomerList, InvoiceLists, etc, so I can embed the logic of the domain in one area and not have the same logic spread throughout the application and ui layers. I really don't want to put all of that logic in Array, plus then I'd have to do a lot of type checking in many cases. Ruby has such beautiful Array functionality that I find I always end up wanting to use it in my customized subclasses. 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 *****************************************************