我想删除通过 include 函数添加到我的类中的类方法。例如:

class Foo
    include HTTParty

    class << self
      remove_method :get
    end
end

这不起作用,它说“get”不是 Foo 上的方法。基本上,HTTParty 模块提供了“get”方法,我想删除它。我尝试了几次都没有运气。我读过/尝试过的事情:
  • Removing/undefining a class method
  • http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/15408
  • http://split-s.blogspot.com/2006/01/removing-methods-from-module.html
  • 最佳答案

    使用 undef 而不是 remove_method :

    require 'httparty'
    
    class Foo
      include HTTParty
      class << self
        undef :get
      end
    end
    
    Foo.get #=> NoMethodError: undefined method `get' for Foo:Class
    

    关于ruby - 删除/取消定义另一个模块包含的类方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7570366/

    10-12 13:10
    查看更多