From: Marnen Laibow-Koser Date: 2009-11-15T04:40:18+09:00 Subject: Re: Naming conventions -- was: Re: DRYing a Regex David A. Black wrote: [...] > I write class methods sometimes (I know those are singleton with an > asterisk next to them, but still), Sometimes they're the right thing... > and I think that extending core > objects with modules is a frequently overlooked and very powerful > alternative to reopening core classes and adding methods. > > This kind of thing: > > class String > def method_I_need_once_or_twice > ... > > is almost always overkill. It's sort of the core-functionality > counterpart of using global variables. True. (Even though I occasionally do this.) > Extending an object is a much > more precise operation -- and has the additional merit, I find, of > really making you think about whether it's worth bothering to the > extend the object instead of working with what the object can already > do. > Agreed. 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. 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? If so, why? I hesitate to contradict such experts as you and James, but I'm really not seeing the benefit. For one thing, type-checking completely falls down with this pattern. Polymorphism may or may not, depending on how it's implemented. And I don't see a single advantage that we get in return for the loss of type-checking. What am I missing? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org > > David > > -- > The Ruby training with D. Black, G. Brown, J.McAnally > Compleat Jan 22-23, 2010, Tampa, FL > Rubyist http://www.thecompleatrubyist.com > > David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com) -- Posted via http://www.ruby-forum.com/.