LoginSignup
0
0

More than 3 years have passed since last update.

Kernelモジュールについて

Last updated at Posted at 2020-05-16

普段何気なく使っているputsは、なんでどういった仕組みで使えるんだろうと気になったので、調べてみました。

結論としては、Kernelモジュールが1枚噛んでいました。

Kernelモジュールが提供するメソッド
puts
p
print
require
gets

上記のようなメソッドは、Kernelモジュールで定義されているらしい。

String、Numeric、Array、Hashなどのクラスは、全てObjectクラスを継承しているみたいです。

親クラスの確認
$ rails c

>> String.superclass
=> Object
>> Numeric.superclass
=> Object
>> Array.superclass
=> Object
>> Hash.superclass
=> Object

もともと、ほぼ全ての親クラスであるObjectクラス(superclassは、BasicObject)は、Kernelモジュールをincludeしているので、
putsメソッドを平然と何も考えなくとも、デフォルトでどのクラスでも使えるみたいです。

以下に具体的に、Kernelモジュールで定義されているメソッドの一覧が載ってます。

module Kernel

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0