From: Mike Gold Date: 2008-10-25T20:00:55+09:00 Subject: Re: Conditionally Including Methods From A Module? Kenny wrote: > Is there a way to include only certain methods from a module instead > of all the methods? Not directly. In general, methods are part of the same module for a reason. A given method may call other methods in the module, and a method may use instance variables defined in other methods. What you propose could make sense in the case of a "pure utility" module which has no state and where there little is or no relationship between the methods. def methods_to_lambdas(mod) instance = Class.new { include mod }.new mod.instance_methods(false).inject(Hash.new) { |hash, meth| hash.update( meth.to_sym => lambda { |*args, &block| instance.send(meth, *args, &block) } ) } end def pull_methods(mod, *names) lambdas = methods_to_lambdas(mod) names.each { |name| define_method(name, &lambdas[name]) } end module Misc def func_a ; p "Misc#func_a" ; end def func_b ; p "Misc#func_b" ; end def func_c ; p "Misc#func_c" ; end end class Fred pull_methods(Misc, :func_a, :func_b) end Fred.new.func_a #=> Misc#func_a Fred.new.func_b #=> Misc#func_b Fred.new.func_c #=> NoMethodError ruby-1.8.7 or 1.9 is needed for the lambda syntax (for 1.8.6 you can eval a string instead). -- Posted via http://www.ruby-forum.com/.