What is the purpose of `Kernel`?

前端 未结 1 1489
刺人心
刺人心 2020-12-16 22:59
  1. What is the purpose of the Kernel module? What would change if all the things currently defined on Kernel were defined on Object
相关标签:
1条回答
  • I'll start with a question: what would self be inside a typical Kernel method such as puts? The closest thing to a meaningful self inside puts would probably be the Ruby runtime itself. Similarly for other "methods that really want to be functions" like Array or fork. So you can look at Kernel as a dumping ground for methods that are, more or less, commands or messages to Ruby itself.

    Kernel also has odd methods like sub and chop that are really only useful for one-off ruby -e scripts. These things tend to use $_ as an implied self but I think they can be considered as special cases of the "commands to the Ruby runtime" as above.

    Where does a method go when you want to be able to call that method on any object? I'd say that it would go into Object. If the method is really a function in disguise and has no meaningful self, then it would go into Kernel.

    0 讨论(0)
提交回复
热议问题