From: ahoward Date: 2002-11-09T00:52:24+09:00 Subject: return MyClass.new vs self.type.send :new Rubyists- I've seen a few threads relating to this subject, so i decided to start one... What do people think is better style def Class def method Class.new end end VS def Class def method self.type.send :new end end the first is safer, but the second can be very nice for derived types since def SubClass < Class end will result in this behavior sub = SubClass.new sub.method.type >> SubClass A recent example of why this might be a good design pattern is that subclasses of Array must re-write all the 'usefull' methods like select, etc. to return the SubClass'd type since these methods return an Array. I can hear the arguments against : "what if the subclassed methods do something perverse with the methods" "what if the subclassed methods simply will not work if that is done" etc. I would say Sub-classing should always be reserved to _extend_ objects, old behavior should always remain - but may be added to. Agregation should be used in most cases. If your class can be broken using this methodology, then perhaps it should not be allowed to be subclassed at all without redesign. Subclassing is abused, and generally much harder to do right than generally realized. The idiom of returning the 'current class'.new is used in perl alot, and it _can_ be very usefull. (probably did not help my argument to say the 'p' word here...) Any thoughts on this? -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ahoward@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================