跳至内容 跳至搜索

DeprecatedInstanceVariableProxy 将一个实例变量转换为一个已弃用的变量。它接受一个类的实例、该类上的一个方法、一个实例变量以及最后一个参数为一个废弃器。

尝试使用已弃用的实例变量将导致弃用警告,并指向该方法作为替换。

class Example
  def initialize
    @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, ActiveSupport::Deprecation.new)
    @_request = :special_request
  end

  def request
    @_request
  end

  def old_request
    @request
  end
end

example = Example.new
# => #<Example:0x007fb9b31090b8 @_request=:special_request, @request=:special_request>

example.old_request.to_s
# => DEPRECATION WARNING: @request is deprecated! Call request.to_s instead of
   @request.to_s
   (Backtrace information…)
   "special_request"

example.request.to_s
# => "special_request"
方法
N

类公共方法

new(instance, method, var = "@#{method}", deprecator:)

# File activesupport/lib/active_support/deprecation/proxy_wrappers.rb, line 88
def initialize(instance, method, var = "@#{method}", deprecator:)
  @instance = instance
  @method = method
  @var = var
  @deprecator = deprecator
end