在Ruby(尤其是Rails)中,将方法标记为已弃用非常容易。
但是我如何将整个类标记为已弃用?我想在使用类时发出警告:
我认为
但是我如何将整个类标记为已弃用?我想在使用类时发出警告:
class BillingMethod
end
BillingMethod.new #=> DEPRECATION WARNING: the class BillingMethod is deprecated. Use PaymentMethod instead.
或者当它用于继承时:
class Sofort < BillingMethod
end
Sofort.new #=> DEPRECATION WARNING: the class BillingMethod is deprecated. Use PaymentMethod instead.
或者,在嵌套类中使用:
class BillingMethod::Sofort < BillingMethod
end
BillingMethod::Sofort.new #=> DEPRECATION WARNING: the class BillingMethod is deprecated. Use PaymentMethod instead.
我认为
class_eval
块是放置这样的警告的地方。那是正确的位置吗?或者还有更好的方法吗?
inherited
钩子(类方法),从而弃用继承:class BillingMethod; class << self; deprecate :inherited, :none, 2016, 4; end; end
。从BillingMethod继承现在应该会发出弃用警告。 - Petr Skocik:new
是一个类方法,所以你需要将extend...deprecate
行包装在class << self
块中。 - berkes