Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rinaldifonseca/2946007 to your computer and use it in GitHub Desktop.
Save rinaldifonseca/2946007 to your computer and use it in GitHub Desktop.

#include and #extend have same effect on instances as inheritance

Let's say we have this

module A; end
module B; end
class C; end
class D < C; end

Then we modify instance

d = D.new
D.send(:include, A)
d.extend(B)
d_meta = class << d; self; end

Lets see what we got (method dispatch lookup table)

d.class.ancestors
=> [D, A, C, Object, Kernel, BasicObject]

d_meta.ancestors
=> [B, D, A, C, Object, Kernel, BasicObject]

So it's all up to how and what structure you see

and to me, benefits of composition over inheritance is that it doesn't modify object dispatch hierarchy.

PS: pls comment here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment