ruby - When I include a module in another which has already been included in a class, the new methods are not accessible to the class -


here code demonstrating behavior i'm talking about:

module   def foo; end end  # initialize b no methods module b; end  class c   include b end  # add methods b module b   def foo; end   include end  c.new.foo c.new.bar # error: instance method bar not defined c 

so defs added b reflected in c, whereas added includes not (unless b re-included afterwards). why this, , there clean way around it?

when include module m in class c, happens:

  1. ruby creates class (let's call ⟦m′⟧) method table pointer, constant table pointer , class variable table pointer point m's method table, constant table , class variable table.
  2. ⟦m′⟧'s superclass pointer set c's superclass.
  3. c's superclass pointer set ⟦m′⟧.

if there modules includeed in m, process applied recursively.

note recursive flattening of mixins applied once, when call include. changes in inheritance hierarchy made afterwards not reflected.

however, when add method m's method table, change reflected, because there one method table, both m , ⟦m′⟧ refer.


Comments