What is the purpose of the Kernel?

  • What is the purpose of the Kernel module? What would change if all things currently defined on Kernel were defined on Object and there wasn’t a module like Kernel ?
  • When I want to define a method that can be called for any object, should I determine what is on Kernel or on Object ?
+7
oop ruby
source share
1 answer

I'll start with the question: what would self be inside a typical Kernel method, for example, puts ? Closest to meaningful self inside puts is likely to be Ruby's most executable environment. Similarly for other “methods that really want to be functions”, such as Array or fork . Thus, you can look at Kernel as a dump for methods that are more or less commands or messages for Ruby itself.

Kernel also has odd methods like sub and chop , which are really only useful for ruby -e one-time scripts. These things usually use $_ as an implied self , but I think they can be considered special cases of “commands for the Ruby runtime environment”, as mentioned above.

Where does the method go when you want this method to be called on any object? I would say that it will go to Object . If the method is really a hidden function and does not make sense self , then it will go to Kernel .

+9
source share

All Articles