Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.
- include: mixes in specified module methods as instance methods in the target class
- extend: mixes in specified module methods as class methods in the target class
So is the major difference just this or is a bigger dragon lurking? e.g.
module ReusableModule def module_method puts 'Module Method: Hi there!' end end class ClassThatIncludes include ReusableModule end class ClassThatExtends extend ReusableModule end puts 'Include' ClassThatIncludes.new.module_method # 'Module Method: Hi there!' puts 'Extend' ClassThatExtends.module_method # 'Module Method: Hi there!'
What you have said is correct. However, there is more to it than that.
If you have a class
Klazzand moduleMod, includingModinKlazzgives instances ofKlazzaccess toMod‘s methods. Or you can extendKlazzwithModgiving the classKlazzaccess toMod‘s methods. But you can also extend an arbitrary object witho.extend Mod. In this case the individual object getsMod‘s methods even though all other objects with the same class asodo not.