module Gem::Deprecate

提供 3 种声明某个内容即将移除的方法。

deprecate(name, repl, year, month):

Indicate something may be removed on/after a certain date.

rubygems_deprecate(name, replacement=:none):

Indicate something will be removed in the next major RubyGems version,
and (optionally) a replacement for it.

rubygems_deprecate_command:

Indicate a RubyGems command (in +lib/rubygems/commands/*.rb+) will be
removed in the next RubyGems version.

还提供了 skip_during 方法,用于临时关闭弃用警告。此方法 intended for use in the test suite, 这样在需要确保 stderr 为空时,弃用警告就不会导致测试失败。

deprecaterubygems_deprecate 的示例用法

class Legacy
  def self.some_class_method
    # ...
  end

  def some_instance_method
    # ...
  end

  def some_old_method
    # ...
  end

  extend Gem::Deprecate
  deprecate :some_instance_method, "X.z", 2011, 4
  rubygems_deprecate :some_old_method, "Modern#some_new_method"

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

rubygems_deprecate_command 的示例用法

class Gem::Commands::QueryCommand < Gem::Command
  extend Gem::Deprecate
  rubygems_deprecate_command

  # ...
end

skip_during 的示例用法

class TestSomething < Gem::Testcase
  def test_some_thing_with_deprecations
    Gem::Deprecate.skip_during do
      actual_stdout, actual_stderr = capture_output do
        Gem.something_deprecated
      end
      assert_empty actual_stdout
      assert_equal(expected, actual_stderr)
    end
  end
end