From: "Ara.T.Howard" Date: 2005-08-02T08:21:04+09:00 Subject: Re: Selective mixins; who can improve on this? On Tue, 2 Aug 2005, Trans wrote: >> There has to be a more elegant way to do this. What is it? >> > > Am interested in other people's solutions too. harp:~ > cat a.rb class Module def import desc desc.each do |a_module, a_list| m = Module::new m.module_eval do include a_module a = a_module.instance_methods b = [ a_list ].flatten.map{|b| b.to_s} (a - b).each{|um| undef_method um} end module_eval{ include m } end end end module Foo def foo p self p 42 end def foobar p self p @foobar end end module Bar def bar p self p 42.0 end def barfoo p self p @barfoo end def not_imported end end class C import Foo => %w( foo foobar ), Bar => %w( bar barfoo ) def initialize @foobar = 'foobar' @barfoo = 'barfoo' end end c = C::new c.foo c.bar c.foobar c.barfoo c.not_imported harp:~ > ruby a.rb # 42 # 42.0 # "foobar" # "barfoo" a.rb:56: undefined method `not_imported' for # (NoMethodError) seems to work. i'm probably missing something though... cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================