module A; end
module B; end
class C; end
class D < C; end
d = D.new
D.send(:include, A)
d.extend(B)
d_meta = class << d; self; end
d.class.ancestors
=> [D, A, C, Object, Kernel, BasicObject]
d_meta.ancestors
=> [B, D, A, C, Object, Kernel, BasicObject]
and to me, benefits of composition over inheritance is that it doesn't modify object dispatch hierarchy.
PS: pls comment here.
That's correct.
Inheritance, include and extend use same technique: modifying the lookup hierarchy.
The difference between them is the location where the features are injected/placed(precedence) in the hierarchy.
Smth like this: extend(B) > class(D) > include(A) > inherit(C).
But i'm not a ruby meta-programming guru, so take above critically )