Skip to content
instance method · added in Ruby 3.2

Module#const_added

(::Symbol) -> void
const_added(const_name)

Invoked as a callback whenever a constant is assigned on the receiver

Example
module Chatty
  def self.const_added(const_name)
    super
    puts "Added #{const_name.inspect}"
  end
  FOO = 1
end

produces:

Example
Added :FOO

If we define a class using the class keyword, const_added runs before inherited:

Example
module M
  def self.const_added(const_name)
    super
    p :const_added
  end

  parent = Class.new do
    def self.inherited(subclass)
      super
      p :inherited
    end
  end

  class Child < parent
  end
end

produces:

Example
:const_added
:inherited

Ruby version changes

  1. 3.2