CSC/ECE 517 Fall 2012/ch1 1w22 an: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 6: | Line 6: | ||
When the object receives a method name that is to be executed, these are the steps carried out for finding out that method called: | When the object receives a method name that is to be executed, these are the steps carried out for finding out that method called: | ||
* It looks in the current self object’s own instance methods. | * It looks in the current self object’s own instance methods. | ||
* Then it looks in the list of instance methods that all objects of that class share. | |||
* Then in each of the included modules of that class, in reverse order of inclusion. | |||
* Then it looks in that class’s superclass. | |||
* Then in the superclass’s included modules, all the way up until it reaches the class Object. | |||
* If it still can’t find a method, the very last place it looks is in the Kernel module, included in the class Object. | |||
* Finally, it calls method_missing (if defined in the class), else throws up the NOMethodError exception. | |||
This entire tracing that the object does is the method lookup path. |
Revision as of 02:57, 9 September 2012
Introduction
When we define a method in a class and decide to call that method, how do we do it?
We simply create an object of the class and pass the method name to the object as a message. The object then looks up into its method lookup path and tries to match the called method (passed as a message to the object) with the defined methods in the class. When there is a match, the method is executed along with the parameters passed and the result is returned.
What is a Method Lookup Path?
When the object receives a method name that is to be executed, these are the steps carried out for finding out that method called:
- It looks in the current self object’s own instance methods.
- Then it looks in the list of instance methods that all objects of that class share.
- Then in each of the included modules of that class, in reverse order of inclusion.
- Then it looks in that class’s superclass.
- Then in the superclass’s included modules, all the way up until it reaches the class Object.
- If it still can’t find a method, the very last place it looks is in the Kernel module, included in the class Object.
- Finally, it calls method_missing (if defined in the class), else throws up the NOMethodError exception.
This entire tracing that the object does is the method lookup path.