Deprecating Constants (and Classes) in Ruby

Ruby has great tools for deprecating obselete code. One of my favorite techniques is one I rarely see: using const_missing to deprecate constants, and, by extension, classes and modules.

Consider this code:

my_gem/config.rb
module MyGem
  class Config
  end
end

At a later time, you decide you would rather name this class MyGem::Configuration. It’s easy enough to change the name, but you may break backwards compatiblity with people who have references to MyGem::Config in their code.

const_missing really comes in handy here:

my_gem/configuration.rb
module MyGem
  class Configuration
  end

  def self.const_missing(const_name)
    super unless const_name == :Config
    warn "`MyGem::Config` has been deprecated. Use `MyGem::Configuration` instead."
    Configuration
  end
end

This allows existing code that references MyGem::Config to continue to work, and warns the user about their use of the deprecated constant.

blog comments powered by Disqus

About Me

Husband and father, musician, software engineer at SEOmoz, open source developer specializing in Ruby and Rails, world traveler and Christian.