summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Noria <[email protected]>2025-02-16 20:30:40 +0100
committerJean Boussier <[email protected]>2025-04-10 10:33:36 +0200
commit08ce6268ee1690f003af40b9357272d43e990d75 (patch)
tree0d04ac6c12879fa0d80e4a387561f1bd51541a1d
parentb47a04eb9151a9fa59979fbaf20deeb404822959 (diff)
Document order of execution const_added vs inherited
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/12759
-rw-r--r--object.c24
-rw-r--r--spec/ruby/core/module/const_added_spec.rb20
2 files changed, 44 insertions, 0 deletions
diff --git a/object.c b/object.c
index 74f58a15bd..5a379e9d08 100644
--- a/object.c
+++ b/object.c
@@ -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