Created
October 26, 2008 22:19
-
-
Save drio/19956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment