What are the differences between:
module Mod
def here
puts 'child'
end
end
class A
prepend Mod
def here
puts 'parent'
end
end
and
class A
def here
puts 'parent'
end
end
class B < A
def here
puts 'child'
end
end
Or another way to put it: is derivating a class the same as prepending a module of the child's code?
No, it is not.
B
can only inherit from one class, butMod
can be prepended to many classes. If you were to callsuper
insideB#here
, it would always refer toA#here
, but inside ofMod#here
, it will refer to the#here
instance method of whatever classMod
was prepended to:and
No, it's totally different.
One can prepend as many modules as he wants.
Ruby implements
prepend
and inheritance using different ways.prepend
is internally achieved by including modules, which causes the surprisingancestors
.here is another question about
prepend
which may be helpful.