When we call a method on an object, Ruby looks for the implementation of that method.
It looks in the following places and uses the first implementation it finds:
- Methods from the object's singleton class (an unnamed class that only exists for that object)
- Methods from prepended modules (Ruby 2.0+ feature)
- Methods from the object's class
- Methods from included modules
- Methods from the class hierarchy (superclass and its ancestors)
class Superclassdef monkputs "rormonk Superclass"endendmodule IncludedModuledef monkputs "rormonk Included module"superendendmodule PrependedModuledef monkputs "rormonk Prepended module"superendendmodule SingletonModuledef monkputs "rormonk Singleton class"superendendclass Klass < Superclassinclude IncludedModuleprepend PrependedModuledef monkputs "rormonk Klass"superendend
kl.monk
Singleton class
Prepended module
Klass
Included module
Superclass
So when I saw first I extend the class and then call the method than obvious it will call that but later I realize if I will not extend that how would I call that 😅
there will be some people like me who could think superclass is mention in every method due to that it is printing like this 😊 so this is for the super is used to maintain the chain so if you will not put super it will not call the other method and it will just return the first method print value.
✌