diff options
author | Xavier Noria <[email protected]> | 2025-02-16 20:30:40 +0100 |
---|---|---|
committer | Jean Boussier <[email protected]> | 2025-04-10 10:33:36 +0200 |
commit | 08ce6268ee1690f003af40b9357272d43e990d75 (patch) | |
tree | 0d04ac6c12879fa0d80e4a387561f1bd51541a1d | |
parent | b47a04eb9151a9fa59979fbaf20deeb404822959 (diff) |
Document order of execution const_added vs inherited
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/12759
-rw-r--r-- | object.c | 24 | ||||
-rw-r--r-- | spec/ruby/core/module/const_added_spec.rb | 20 |
2 files changed, 44 insertions, 0 deletions
@@ -1193,6 +1193,30 @@ rb_class_search_ancestor(VALUE cl, VALUE c) * * Added :FOO * + * If we define a class using the <tt>class</tt> keyword, <tt>const_added</tt> + * runs before <tt>inherited</tt>: + * + * 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 + * + * <em>produces:</em> + * + * :const_added + * :inherited */ #define rb_obj_mod_const_added rb_obj_dummy1 diff --git a/spec/ruby/core/module/const_added_spec.rb b/spec/ruby/core/module/const_added_spec.rb index 4b10dd5963..1b3dad514b 100644 --- a/spec/ruby/core/module/const_added_spec.rb +++ b/spec/ruby/core/module/const_added_spec.rb @@ -199,5 +199,25 @@ describe "Module#const_added" do ScratchPad.recorded.should == [123, 456] end + + it "for a class defined with the `class` keyword, const_added runs before inherited" do + ScratchPad.record [] + + mod = Module.new do + def self.const_added(_) + ScratchPad << :const_added + end + end + + parent = Class.new do + def self.inherited(_) + ScratchPad << :inherited + end + end + + class mod::C < parent; end + + ScratchPad.recorded.should == [:const_added, :inherited] + end end end |