ruby-on-rails - 弃用 Rails 中所有类方法的最佳方法

标签 ruby-on-rails ruby deprecated class-method deprecation-warning

我有一个具有多个类方法的类(它不是 ActiveRecord 模型)。必须弃用所有类方法。执行此操作的最佳方法是什么?

class MyClass
  class << self
    def method_to_deprecate_1
      ...
    end
    ...
    def method_to_deprecate_100
      ...
    end
  end
end

最佳答案

Ruby 有一个特殊的模块 Gem::Deprecate为此,这是官方文档中的示例:

class Legacy
  def self.klass_method
    # ...
  end

  def instance_method
    # ...
  end

  extend Gem::Deprecate
  deprecate :instance_method, "X.z", 2011, 4

  class << self
    extend Gem::Deprecate
    deprecate :klass_method, :none, 2011, 4
  end
end

它会导致:

2.5.0 :020 > Legacy.new.instance_method
NOTE: Legacy#instance_method is deprecated; use X.z instead. It will be removed on or after 2011-04-01.
Legacy#instance_method called from (irb):20.
 => nil 
2.5.0 :021 > Legacy.klass_method
NOTE: Legacy.klass_method is deprecated with no replacement. It will be removed on or after 2011-04-01.
Legacy.klass_method called from (irb):21.
 => nil

编辑: 要直接回答您的问题,这是我能想到的最优雅的弃用所有类方法的方法:

class Kek
   class << self
     def old_method_1
       # ...
     end

     def old_method_2
       # ...
     end

     extend Gem::Deprecate

     # instance methods here are our actual class methods + all of the Object's methods from Ruby
     instance_methods(false).each { |method_to_deprecate| deprecate(method_to_deprecate, :none, 2011, 4) }
   end
end

关于ruby-on-rails - 弃用 Rails 中所有类方法的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66228836/

相关文章:

html - Css float : left; drops further on every item

ruby-on-rails - 执行 rake 命令时无法理解 zsh 错误

ruby - 无法在 Linux 上安装 jekyll gem(classifier-reborn ruby​​ 版本错误)

具有指定开始和结束的序列的 Ruby 正则表达式

java - 为什么大多数 java.util.Date 方法都被弃用了?

caching - MySQL 8 警告 : SQL_NO_CACHE is deprecated

mysql - 无法在 Mac OS X 上安装 MySQL

ruby-on-rails - ruby rails : How do you add add zeros in front of a number if it's under 10?

ruby-on-rails - % 在下面的代码中做了什么?

java - 抑制 Java 中不推荐使用的导入警告