instance method · added in Ruby 3.2
Module#const_added
(::Symbol) -> void
const_added(const_name)
object.c
View on GitHub ↗
#define rb_obj_mod_const_added rb_obj_dummy1
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
-
3.2
-
Module#const_addedhas been added. [Feature #17881]
-
See also
#class_variable_set
#constants
#class_variables
#const_defined?
also in
4.1 · 3.4 · 3.3 · 3.2