我想删除通过 include
函数添加到我的类中的类方法。例如:
class Foo
include HTTParty
class << self
remove_method :get
end
end
这不起作用,它说“get”不是 Foo 上的方法。基本上,HTTParty 模块提供了“get”方法,我想将其删除。我尝试了几次都没有运气。我读过/试过的东西:
最佳答案
使用undef
代替remove_method
:
require 'httparty'
class Foo
include HTTParty
class << self
undef :get
end
end
Foo.get #=> NoMethodError: undefined method `get' for Foo:Class
Cancels the method definition. Undef can not appear in the method body. By using undef and alias, the interface of the class can be modified independently from the superclass, but notice it may be broke programs by the internal method call to self. http://web.njit.edu/all_topics/Prog_Lang_Docs/html/ruby/syntax.html#undef
关于ruby - 删除/取消定义另一个模块包含的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7570366/