So I understand that test_fun definition refers to the current class
which is Object and is added to the instance_methods but I don't
understand why this also appears in the Kernel module methods.
You wrote Kernel.methods, not Kernel.instance_methods. Kernel.methods
returns the names of all the methods on the Kernel object itself. "Kernel" is
a reference to an object of class Module, which is a subclass of Object.
Thus, since test_fun is an instance method on Object, and Kernel is an instance
of a subclass of Object, it has access to that method as well.
I think you were confused between Kernel.methods and Kernel.instance_methods.
Kernel.instance_methods does not include "test_fun" given this example, as expected.
Almost everything in ruby is an object. All objects inherit from
Object. Kernel is a class, but Kernel itself is also an object, so it,
like all objects, inherits from Object. Therefore, all instance methods
in Object are inherited by Kernel
Note the circularity: Object includes Kernel, so all the Kernel methods
are instance methods of Object. As noted above, Kernel inherits from
Object, so Kernel actually inherits all its own instance methods.
Thanks everyone for the explanation, I just got confused and forgot that
Kernel was also an object...
BTW, any reason for the change in later version to have method
definition at the top-level scope being created in the private methods
space. That only seems specific to top-level scope. I am just wondering
about why the special case here...
The top-level scope creates private methods by default.
I know that.
Then why did you add the "test_fun" private method to Object,
then print Object's public instance methods starting with t?
Object.private_instance_methods is what you want.
I don't want anything.
If your printout had nothing to do with the "test_fun" method, then all
it seemed to illustrate was that different versions of Ruby provide
different methods. Was there something else to be gleaned that
I'm missing?