Skip to content

Commit 08ce626

Browse files
fxnbyroot
authored andcommitted
Document order of execution const_added vs inherited
1 parent b47a04e commit 08ce626

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

object.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,30 @@ rb_class_search_ancestor(VALUE cl, VALUE c)
11931193
*
11941194
* Added :FOO
11951195
*
1196+
* If we define a class using the <tt>class</tt> keyword, <tt>const_added</tt>
1197+
* runs before <tt>inherited</tt>:
1198+
*
1199+
* module M
1200+
* def self.const_added(const_name)
1201+
* super
1202+
* p :const_added
1203+
* end
1204+
*
1205+
* parent = Class.new do
1206+
* def self.inherited(subclass)
1207+
* super
1208+
* p :inherited
1209+
* end
1210+
* end
1211+
*
1212+
* class Child < parent
1213+
* end
1214+
* end
1215+
*
1216+
* <em>produces:</em>
1217+
*
1218+
* :const_added
1219+
* :inherited
11961220
*/
11971221
#define rb_obj_mod_const_added rb_obj_dummy1
11981222

spec/ruby/core/module/const_added_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,25 @@ def self.const_added(name)
199199

200200
ScratchPad.recorded.should == [123, 456]
201201
end
202+
203+
it "for a class defined with the `class` keyword, const_added runs before inherited" do
204+
ScratchPad.record []
205+
206+
mod = Module.new do
207+
def self.const_added(_)
208+
ScratchPad << :const_added
209+
end
210+
end
211+
212+
parent = Class.new do
213+
def self.inherited(_)
214+
ScratchPad << :inherited
215+
end
216+
end
217+
218+
class mod::C < parent; end
219+
220+
ScratchPad.recorded.should == [:const_added, :inherited]
221+
end
202222
end
203223
end

0 commit comments

Comments
 (0)