1 class Module
    2   # Call this hook early in your program to mix in all the automatic
    3   # mixins.  On second thought, call this late in your program maybe :) 
    4   # Since classes and modules can be extended any time, the decision
    5   # as to whether a class meets some mixin criterion can change!
    6   def Module.AddAutomaticMixins
    7     ObjectSpace.each_object(Class) do |c|
    8       ObjectSpace.each_object(Module) do | mod | 
    9        mod.add_automatic_mixin( c)
   10       end
   11     end
   12   end
   13 
   14   protected
   15   def add_automatic_mixin( other)
   16     # note: it doesn't hurt to add a mixin twice
   17     other.class_eval "include #{self}" if( @mix_in_when && other.instance_eval( &@mix_in_when))
   18   end
   19   
   20   private  
   21   # Call this to mark modules you write so that they will
   22   # be automatically mixed-in to every new class that meets your
   23   # criteria.  Criteria is expressed in the block parameter.
   24   def mix_in_when( &predicate) @mix_in_when = predicate; end
   25 end
   26 
   27 # Mark the core library mixins as automatic mixins
   28 module Comparable
   29   mix_in_when { method_defined? :<=> }
   30 end
   31 module Enumerable
   32   mix_in_when { method_defined? :each }
   33 end