[Nitro] Better than classinherit.rb!

George Moschovitis george.moschovitis at gmail.com
Mon Apr 10 13:00:04 EDT 2006


Wow ;-)

-g.

PS: funny name btw ;-)

On 4/10/06, TRANS <transfire at gmail.com> wrote:
> 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.
>
> _______________________________________________
> Nitro-general mailing list
> Nitro-general at rubyforge.org
> http://rubyforge.org/mailman/listinfo/nitro-general
>


--
http://www.gmosx.com
http://www.navel.gr
http://www.nitrohq.com




More information about the Nitro-general mailing list