Hi --

On Wed, 4 Feb 2009, Robert Dober wrote:

> On Tue, Feb 3, 2009 at 2:44 PM, David A. Black <dblack / rubypal.com> wrote:
>> On Tue, 3 Feb 2009, Thomas Sawyer wrote:
>>
>>  class << obj
>>    def x
>>    end
>>  end
>>
>>  module M
>>    def x
>>    end
>>  end
>>  obj.extend(M)   # obj.x is still the first one
>>
>> Since you can always reopen the singleton class and redefine the
>> method, using the singleton class in the first place doesn't mean that
>> you're stuck with the original method definition.
>>
>> I think #extend is highly underutilized. It's one of my favorite Ruby
>> techniques. I just don't feel that it has to be in a death-struggle
>> with any other techniques.
>
> Well is that not exactly what Tom meant? Maybe someone can update me
> here please?
> My understanding is that:
>
> If people would use extend instead of class << etc the
> "metaprogrammers" (e.g. somebody creating a framework or an extensions
> for traits as I did ) could produce code easier just by saying:
>   As long as you extend your object and classes and never use << my
> framework is shielded. If you reopen singleton classes, well you are
> at your own.

It depends what you mean by shielded. If I use a library that extends
my objects with modules, and then I extend those same objects with
other modules that have methods of the same name, then the methods in
the library will be hidden. This is, however, a very unlikely
scenario. But avoiding << isn't a cure-all, if the problem is that two
modules are fighting over a particular method for a particular object.

Nor is this kind of problem specific to singleton methods and
singleton classes, of course. This is why I think it's very tricky to
come up with rules in this area. In the end, it's always going to be
about particular code and particular programming goals.


David

-- 
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!