moduleM# Methods that you want to call only within this contextrefineMdodefrefine_method"refine_method"endendusingMdefhoge# OK: `refine_method` calls the methodrefine_methodendendclassXincludeMendppX.new.hoge# => "refine_method"
If prepend another module, the search for refine_method fails
moduleOtherMendmoduleM# Added prependprependOtherMrefineMdodefrefine_method"refine_method"endendusingMdefhoge# Error: `hoge': undefined local variable or method `refine_method' for #<X:0x00007fa05a024390> (NameError)refine_methodendendclassXincludeMend# ErrorppX.new.hoge
This issue is specific to modules that are refined and use prepend. The reason it does not work:
Refining a module or class that prepends other modules places the refinements in the class itself and not the origin iclass.
Inclusion of a module that prepends other modules skips the module itself, including only iclasses for the prepended modules and the origin iclass.
Those two behaviors combined meant that the method table for the refined methods for the included module never ends up in the method lookup chain for the class including the module.
Fix this by not skipping the module itself when the module is included (see https://github.com/ruby/ruby/pull/2550). This requires some code rearranging in rb_include_class_new to make sure the correct method tables and origin settings are used for the created iclass.
As origin iclasses shouldn't be exposed to Ruby, this also requires skipping modules that have origin iclasses in Module#ancestors (classes that have origin iclasses were already skipped).
Honor refinements for modules that prepend other modules
This previously did not work, and the reason it did not work is
that:
Refining a module or class that prepends other modules places
the refinements in the class itself and not the origin iclass.
Inclusion of a module that prepends other modules skips the
module itself, including only iclasses for the prepended modules
and the origin iclass.
Those two behaviors combined meant that the method table for the
refined methods for the included module never ends up in the
method lookup chain for the class including the module.
Fix this by not skipping the module itself when the module is
included. This requires some code rearranging in
rb_include_class_new to make sure the correct method tables and
origin settings are used for the created iclass.
As origin iclasses shouldn't be exposed to Ruby, this also
requires skipping modules that have origin iclasses in
Module#ancestors (classes that have origin iclasses were already
skipped).