当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Deprecate模块用法及代码示例


本文简要介绍ruby语言中 Gem::Deprecate模块 的用法。

提供 3 种方法来声明某事何时消失。

+弃用(名称,repl,年,月)+:

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

+rubygems_deprecate(名称,替换=:无)+:

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 用于暂时关闭弃用警告。这旨在用于测试套件,因此如果您需要确保 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

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Deprecate模块。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。