This thread led me to write this: # = capsule.rb # # == Copyright (c) 2006 Thomas Sawyer # # Ruby License # # This module is free software. You may use, modify, and/or redistribute this # software under the same terms as Ruby. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. # # == Authors & Contributors # # * Thomas Sawyer # Author:: Thomas Sawyer # Copyright:: Copyright (c) 2005 Thomas Sawyer # License:: Ruby License # = Capsule # # A Capsule encapsulates reusable code and is an analog to the Module class, # but rather than storing it's own methods it simply stores modules. These # modules are divided into two groups: extensions and inclusions. A capsule # is reused via the Kernel#inherit method. # # == Usage # # module Foo # def foo ; "foo" ; end # end # # module Bar # def bar ; "bar" ; end # end # # C = Capsule.new do # extend Foo # include Bar # end # # class X # inherit X # end # # X.foo #=> "foo" # X.new.bar #=> "bar" # # Use #[] as a shortcut for grouping one inclusion and one extension together into # a new capsule. # # C = Capsule[ Foo, Bar ] # class Capsule attr_reader :extensions, :inclusions def initialize( &template ) @extensions = [] @inclusions = [] instance_eval &template if template end # Shortcut for creating an inclusion-extension pair capsule. def self.[]( e, i ) new << e < i end # Add modules to the inclusions group. def include( *inclusions ) @inclusions.concat inclusions end # Add modules to the extensions group. def extend( *extensions ) @extensions.concat extensions end # Add a module to the inclusions group. def <( inclusion ) @inclusions << inclusion self end # Add a module to the extensions group. def <<( extension ) @extensions << extension self end # Append this capsules features to a given base module or class. def append_features( base ) # use #instance_exec in Ruby 1.9+ base.send( :extend, *@extensions ) base.send( :include, *@inclusions ) end end class Module # Inherit behavior from other modules or capsules. def inherit( *capsules ) capsules.each do |c| c.append_features( self ) end end end No doubt it can be improved on, but it clearly ties together what Ruby presently does and the behvior some of us would like to achieve. T.