James Edward Gray II wrote:
> On Nov 14, 2009, at 1:40 PM, Marnen Laibow-Koser wrote:
> 
>> 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.

OK.  That makes more sense to me than the sample I gave.

> 
>> 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, 

Even subclassing like
class SpecialString < String
  include SpecialModule
end
?

It seems to me that this is the best of both worlds.

>it does participate in the 
> type system,

True.  I hadn't considered singleton module inclusion as completely as I 
should have.

> it can be documented.  

How can it be documented?  In the example I gave above, the rdoc for 
SpecialString will say that it includes SpecialModule.  I don't see how 
to do that with a singleton mixin.

> I'm not seeing the minuses.

Start with the lack of documentation.

Also, I admit that something feels deeply *wrong* to me about extending 
individual objects (on more than an occasional basis) without changing 
their nominal class.  I think a class is still a useful construct, but 
too much singleton manipulation renders it meaningless -- a class seems 
to me like a rule for how an object will act, and singleton manipulation 
creates too many exceptions to the rules.  That feels like sloppy, 
unstructured programming to me, although I'll be interested to try the 
pattern in practice and see what comes of it.

> 
> James Edward Gray II

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen / marnen.org
-- 
Posted via http://www.ruby-forum.com/.