如何从Ruby模块中仅导入几个函数?
发布时间:2020-12-17 04:24:24 所属栏目:百科 来源:网络整理
导读:假设我有一个带有方法的模块:function1,function2,function3.我想导入function1和function2但不导入function3.有没有办法在ruby中做到这一点? 解决方法 不确定是否有一种干净的方法来添加所需的方法,但是您可以使用undef_method删除不需要的方法. module F
假设我有一个带有方法的模块:function1,function2,function3.我想导入function1和function2但不导入function3.有没有办法在ruby中做到这一点?
解决方法
不确定是否有一种干净的方法来添加所需的方法,但是您可以使用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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |