禁用gem中未使用的方法

时间:2013-12-10 05:33:41

标签: ruby gem

我正在尝试禁用我的rails应用程序中存在的gem中未使用的方法。有可能吗?

3 个答案:

答案 0 :(得分:0)

您可以使用remove_method http://apidock.com/ruby/Module/remove_method

我很好奇你为什么要这样做。

答案 1 :(得分:0)

您可以覆盖该方法,让它的行为方式不同,或者以下方为ruby:

undef_method http://ruby-doc.org/core-2.0.0/Module.html#method-i-undef_method

remove_method http://ruby-doc.org/core-2.0.0/Module.html#remove_method-method

答案 2 :(得分:0)

如果要从特定类(而不是祖先)中删除方法,则应使用remove_method

如果您还要搜索接收器的超类和混合模块,请使用undef_method

还有undef关键字与remove_method类似,但可能会更快一些。它接收方法名称(不是符号或字符串)。

<强>用法:

class Parent
  def foo; end

  def baz; end
end

class Child < Parent
  def bar; end
end

Child.send :remove_method, :bar # I use send for space-economy. 
                                # You should reopen the class
Child.new.bar # => NoMethodError
Child.send :remove_method, :foo # NameError: method `foo' not defined in Child

Child.send :undef_method, :foo
Child.new.foo # => NoMethodError

Parent.class_eval { undef baz } 
Parent.new.baz # => NoMethodError