[Nitro] Better than classinherit.rb!

TRANS transfire at gmail.com
Mon Apr 10 12:05:47 EDT 2006


Thanks to a couple of posts on Ruby-talk, especially one about using
cattr in modules, I believe I've surpassed myself with this gem that
should (unless I missed something) be able to usurp classinherit.rb.

Thing is, once again I gotta come up with a beter name!


  require 'facets/core/module/basename'

  class Module

    def interlude( mod )
      ext = Module.new  # extension module
      const_set( "Ex#{mod.basename}", ext )
      smethods = mod.singleton_methods
      smethods -= ['included'] if Class === mod
      smethods.each do |m|
        ext.class_eval do
          define_method(m, &(mod.method(m)))
        end
      end
      extend ext unless smethods.empty?
      include mod
      nil
    end

  end

try it:

  module M
    def self.s ; 1 ; end
    def s ; 2 ; end
  end

  module Q
    interlude M
  end

  class R
    interlude Q
  end

  R.s      #=> 1
  R.new.s  #=> 2

T.




More information about the Nitro-general mailing list