"hello".class # => String
String.class # => Class
inherited = false
Class.instance_methods(inherited) # => [:superclass, :allocate, :new]
String.superclass # => Object
Object.superclass # => BasicObject
BasicObject.superclass # => nil
Class.superclass # => Module
Module.superclass # => Object
ClassとModuleはちょっと特別だ。
ObjectがKernelをincludeしている。
Object.class # => Class
BasicObject.class # => Class
Class.class # => Class
Module.class => Class
2.0.0p247 :013 > String.ancestors
=> [String, Comparable, Object, Kernel, BasicObject]
ComparableとKernelはModule
他の例
2.0.0p247 :028 > module M
2.0.0p247 :029?> def my_method
2.0.0p247 :030?> 'M#mymethod'
2.0.0p247 :031?> end
2.0.0p247 :032?> end
=> nil
2.0.0p247 :033 > class C
2.0.0p247 :034?> include M
2.0.0p247 :035?> end
=> C
2.0.0p247 :036 > class D < C; end
=> nil
2.0.0p247 :037 > D.new.my_method()
=> "M#mymethod"
2.0.0p247 :038 > D.ancestors
=> [D, C, M, Object, Kernel, BasicObject]
MとKernelはModule
BasicObject
↑
(Module)Kernel # included by Object
↑
Object
↑
(Module)M # included by C
↑
C
↑
D
load & require
- loadはコードを実行するため
- requireはlibraryをimportするため