ruby - 删除/取消定义另一个模块包含的类方法

标签 ruby metaprogramming

我想删除通过 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/

相关文章:

html - Bootstrap nav nav-pills ruby​​ on rails,不切换

ruby 哈希 : getting a set of keys from sorted values

ruby-on-rails - 属于同一模型 Rails 的模型实例

ruby - 需要帮助来理解这段代码

metaprogramming - Elixir 中的宏可以覆盖函数应用程序吗?

ruby - 在运行时获取/设置 Ruby 方法元数据的最佳策略是什么?

ruby-on-rails - 将 Bundler 与 Git 结合使用,是 https ://or git://better?

ruby-on-rails - Rails 和 capistrano 部署 : bundle stderr: Nothing written

rust - 如何在 Rust 中测试类型相等性?

c++ - 根据某些元数据在运行时转换参数包值