Module
26.3
Lesson

Modules are not copy paste

Let’s see what happens if we change a module after it has been included.

As we can see the result is “Mike Bohanan”. Do not think of including a module as including all the code of module into the class. This is not like all the code in module is copy pasted in the class. Including a module does not work like that.

Including a module is more like setting up a link in the method lookup. When p1 is looking for method name then first ruby will check if class Person has any instance method called name. Answer is No. Then Ruby will check if class Person includes any modules. Answer is Yes. Module Info is include. Then Ruby checks if that module has a method called name. Answer is yes. Ruby executes that method.

That is why we can make changes to the module even after they are included in a class and the updated method will be picked up.