We know what the self of the top-level domain is?
Puts self # mainputs self. class # object
We know that when a method is called, if no object is accepted, the default value is self, for example:
Def tell_me_who puts selfendtell_me_who # Main
Method calling is a step like this. First, check whether the instance method of the current object's class has a method. If so, call the method. If not, view the superclass, until basicobject does not find the method, it will call the method_missing () method of the kernel and report an error, as shown in
Error: test. RB: 8: undefine: undefined local variable or method 'Ask 'for main: Object (nameerror)
Note the error message. We can find that when we call a non-existent variable, the method_missing method of the kernel will be traced back.
Verification:
Puts self # mainputs self. class # objectdef self. method_missing (name, * Arg) puts "# {name} is not exist! "Endputs ask # ask is not exist!
A case causes a bug:
Def self. method_missing (name, * Arg) 1. Times do puts method_name = Name end puts "# {method_name} is not exist! "Endask # VARIABLES or methods
Intent: print all undefined variables or methods once.
But is this an endless loop? See the problem?
Ask is executed, but the definition of ask is not defined, it will be transferred to method_missing,
Method_name is out of scope in the Times block, so method_missing is executed again, which turns into an endless loop.