On Tue, 13 Jun 2006, Sean O'Halpin wrote:

> On 6/12/06, ara.t.howard / noaa.gov <ara.t.howard / noaa.gov> wrote:
>> what did you think about my solution matz?  it's solves the issue cleanly 
>> and
>> is leverages existing practices.  here it is again:
>
> I for one like it - pragmatic and straightforward. However, I'm not
> sure why you want the InstanceMethods module (unless it's to be
> explicit).
>
> Regards,
> Sean

   module M
     include Inheritable
     module ClassMethods
       def foo() 'foo' end
     end
     module InstanceMethods
       def bar() 'bar' end
     end
   end

   class A
     include M # here we get everything due to overridden self.include
   end

   class B
     include M::InstanceMethods # here we cherry pick
   end

   class C
     extend M::ClassMethods # also cherry picking
   end


if, instead we were to do

   module M
     include Inheritable
     module ClassMethods
       def foo() 'foo' end
     end

     def bar() 'bar' end
   end

then this also works with Inheritable

   class A
     include M # here we get everything due to overridden self.include
   end

since the overridden self.include calls super, but you lose the ability to
cherry pick the instance methods.  Note that the InstanceMethods modules is
totally optional and only makes sense if you plan on cherry picking later -
which i've actually done three for four times every.  in pratice my own
inheritable.rb looks like this:

   module Inheritable
     Inherit = lambda do |this, other|
       cm = this.const_get 'ClassMethods' rescue nil
       im = this.const_get 'InstanceMethods' rescue nil
       m = this.const_get 'Methods' rescue nil
       other.extend cm if cm
       other.extend m if m
       other.module_eval{
         include im if im
         include m if m
         extend RecursiveInherit
       }
     end

     module RecursiveInherit
       def included other
         Inherit[self, other]
         super
       end
     end

     extend RecursiveInherit
   end


so the rules are

   ClassMethods    => class methods only
   InstanceMethods => instance methods only
   Methods         => methods that can be called at class or instance level
   (none)          => 'normal' instance methods, no cherry picking

this covers all the bases and make sense even to newbies reading the code.
90% of all cases will only use ClassMethods, however.

cheers.


-a
-- 
suffering increases your inner strength.  also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama