如何从Ruby模块中仅导入几个函数? [英] How can I import only a couple of functions from a Ruby module?
本文介绍了如何从Ruby模块中仅导入几个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个带有方法的模块:function1,function2,function3.我想导入function1和function2,但不导入function3.有没有办法在红宝石中做到这一点?
Suppose I have a module with the methods : function1,function2,function3. I want to import function1 and function2 but not function3. Is there a way to do this in ruby?
推荐答案
不确定是否有一种干净的方法可以只添加所需的方法,但是可以使用undef_method
删除不需要的方法
Not sure if there is a clean way to just add the methods you want, but you can remove the methods that you don't want by using undef_method
.
module Foo
def function1
end
def function2
end
def function3
end
end
module MiniFoo
include Foo
not_wanted_methods = Foo.instance_methods - %w(function1 function2)
not_wanted_methods.each {|m| undef_method m}
end
class Whatever
include MiniFoo
end
这篇关于如何从Ruby模块中仅导入几个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文