deprecate_constant
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- 7.2.3 (38)
- 8.0.0 (0)
- 8.1.1 (0)
- What's this?
deprecate_constant(old_constant_name, new_constant_path, deprecator:, message: nil)
public
Provides a way to rename constants with a deprecation cycle in which both the old and new names work, but using the old one prints a deprecation message.
In order to rename A::B to C::D, you need to delete the definition of A::B and declare the deprecation in A:
require "active_support/deprecation" module A include ActiveSupport::Deprecation::DeprecatedConstantAccessor deprecate_constant "B", "C::D", deprecator: ActiveSupport::Deprecation.new end
The first argument is a constant name (no colons). It is the name of the constant you want to deprecate in the enclosing class or module.
The second argument is the constant path of the replacement. That has to be a full path even if the replacement is defined in the same namespace as the deprecated one was.
In both cases, strings and symbols are supported.
The deprecator keyword argument is the object that will print the deprecation message, an instance of ActiveSupport::Deprecation.
With that in place, references to A::B still work, they evaluate to C::D now, and trigger a deprecation warning:
DEPRECATION WARNING: A::B is deprecated! Use C::D instead. (called from ...)
The message can be customized with the optional message keyword argument.
For this to work, a const_missing hook is installed. When client code references the deprecated constant, the callback prints the message and constantizes the replacement.
Caveat: If the deprecated constant name is reachable in a different namespace and Ruby constant lookup finds it, the hook won’t be called and the deprecation won’t work as intended. This may happen, for example, if an ancestor of the enclosing namespace has a constant with the same name. This is an unsupported edge case.

