On Tue, 11 Apr 2006, Sam Roberts wrote:

> On Tue, Apr 11, 2006 at 02:46:24AM +0900, zdennis wrote:
>>> Is there an easy way to extend classes with both class and object
>>> methods at the same time?
>
>>   irb(main):010:1*   def self.included( clazz )
>>   irb(main):011:2>      clazz.extend( ClassLevelAddons )
>>   irb(main):012:2>   end
>>   irb(main):013:1> end
>>
>> The above lets M take care of the work, so your class doesn't have to
>> think about it. This is the method that Robert Klemme was refering to,
>> Module::included
>
> I was hoping that I was missing some clever way to do it with just one
> module. Guess not. But the self.included hook is a good idea.  The two
> modules become an implementation detail, and thats good enough.

you can do it with one:


   harp:~ > cat a.rb
   module M
     def a
       self
     end
     def self.included other
       other.extend self
       super
     end
   end

   class C
     include M
   end

   p C.a
   p C.new.a


   harp:~ > ruby a.rb
   C
   #<C:0xb75d0a20>


regards.

-a
-- 
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama