From: James Edward Gray II Date: 2009-11-15T05:36:03+09:00 Subject: Re: Naming conventions -- was: Re: DRYing a Regex On Nov 14, 2009, at 1:40 PM, Marnen Laibow-Koser wrote: > But why is it worth extending an object instead of subclassing it? > > In other words, > class SpecialString < String > def method_I_need_once_or_twice > ... > end > ... > @some_string = SpecialString.new(@some_string) > > seems to me like the right way to do this -- you can check if a given > String instance is a SpecialString or a plain String, or simply use > overriding and polymorphism for delegation. If you mix a module into the String, you can still use is_a?() to check for that module. It can also override methods. The object is still polymorphic. I'm not understanding the advantages you think we are losing. > Yet, if I understand you correctly, you and James are claiming that it > is preferable to do > class << @some_string > def method_I_need_once_or_twice > end > > Do I understand correctly? I would much prefer to mix a module into the object. It has all the advantages of changing a singleton class, but it participates in the type system, can be read by RDoc, etc. > If so, why? Hopefully I've made my case by now, but just to sum it up one more time: I think it's more flexible than subclassing, it does participate in the type system, it can be documented. I'm not seeing the minuses. James Edward Gray II