それはおそらくまったくパラドックスではありませんが、初心者の観点からは、確かにそのように見えます.
> Class.superclass
=> Module
> Class.superclass.class
=> Class
> Class.superclass.class.superclass
=> Module
クラスの親はモジュールですが、モジュールはクラスですか?
どうすればこれを理解できますか?
それはおそらくまったくパラドックスではありませんが、初心者の観点からは、確かにそのように見えます.
> Class.superclass
=> Module
> Class.superclass.class
=> Class
> Class.superclass.class.superclass
=> Module
クラスの親はモジュールですが、モジュールはクラスですか?
どうすればこれを理解できますか?
TL;DR: Module はClassのスーパークラスです。Module はClass のインスタンスです。
Ruby のすべてのクラスには 1 つのスーパークラス* があります。
*スーパークラスを持たない BasicObject を除く。
上の図を次のように読みます。 Float のスーパークラスは Numeric です。Numeric のスーパークラスは Object などです。
オブジェクトをインスタンス化すると、オブジェクトは何らかのクラスのインスタンスになります。たとえば、「Nathan」は String クラスのインスタンスです。「ジョー」や「ジョン」もそうです。1 は Fixnum クラスのインスタンスであり、2、3、4 なども同様です...
上の図を次のように読んでください。「Joe」は String のインスタンスです。1 は Fixnum などのインスタンスです...
Ruby では、他のほとんどの言語とは異なり、Class は単なる別のクラスであり、Fixnum や String のようにインスタンス化することもできます。
上の図を次のように読んでください。0.01 は Float のインスタンスです。文字列はクラスなどのインスタンスです...
"Nathan" が String のインスタンスであるように、Fixnum は Class のインスタンスであることに注意してください。"John" が String のインスタンスであるように、Float は単なる Class のインスタンスです。すべてのクラスは Class の単なるインスタンスであり、Class 自体も含まれます!
アプリで新しいクラスを作成するときはいつでも、Hash.new が新しいハッシュをインスタンス化したり、"Nathan" が新しい文字列をインスタンス化したりするのと同じように、クラスが Class である新しいオブジェクトをインスタンス化しているだけです。
# By running this, you will be instantiating a new Class, and
# it will be named Post
class Post < ActiveRecord::Base
end
# Here is another perfectly valid way to write the above code:
Post = Class.new(ActiveRecord::Base)
# you can even instantiate a Class without giving it an explicit name:
x = Class.new(ActiveRecord::Base)
# and since your new things are classes, they can be instantiated
obj1 = Post.new
obj2 = x.new
さらに、Module は Class の単なる別のインスタンスです。アプリで新しいモジュールを作成するときはいつでも、新しいモジュールをインスタンス化しているだけです。
# this will instantiate a new Module, and assign it to Foo
module Foo
end
# Here is another perfectly valid way to write the above code:
Foo = Module.new
# you can even instantiate a Module without giving it an explicit name.
m = Module.new
余談ですが、モジュールはメソッドと定数の単なるコレクションです。クラスもメソッドと定数のコレクションですが、インスタンス化できる機能が追加されています。モジュールをインスタンス化できません。つまり、機能しm.new
ません。
したがって、上の図を参照すると、あなたの質問に直接答えることができます。
クラスの親はモジュールですが、モジュールはクラスですか?
上の図からわかるように、Module はClassのスーパークラスです。
下の図から: Module はClassのインスタンスです。
2 番目の例 Class.superclass.class では、Module.class を呼び出しています。Module はクラス、つまり Module クラスを指します。
AnyClass.superclass.class
を除いてクラスを返しますBasicObject.superclass.class
クラスとインスタンスの違いは重要です。BasicObject はクラスです。これは nil を拡張したクラスであり、スーパークラスを持たないことを巧妙に表現したものです。木の根です。EVERYTHING はオブジェクトです。これは、すべてが何らかのクラスのインスタンスであるという別の言い方です。
文字列の例
"Nathan" is an object.
"Nathan" is an instance of the String class.
"Nathan" is not a class.
"Nathan" has no superclass, because "Nathan" is not a class.
String is an object.
String is an instance of the Class class.
String's superclass is Object.
Object is an object.
Object is an instance of the Class class.
Object's superclass is BasicObject.
BasicObject is an object.
BasicObject is an instance of the Class class
BasicObject's superclass is nil.
nil is an object.
nil is an instance of the NilClass class
nil has no superclass, because it is not a class.
固定番号の例
1 is an object.
1 is an instance of the Fixnum class.
1 is not a class.
1 has no superclass, because it is not a class.
Fixnum is an object.
Fixnum is an instance of the Class class.
Fixnum's superclass is Integer.
Integer is an object.
Integer is an instance of the Class class
Integer's superclass is Numeric.
Numeric is an object.
Numeric is an instance of the Class class.
Numeric's superclass is Object.
# everything below here is in the above example.
Object is an object.
Object is an instance of the Class class.
Object's superclass is BasicObject.
BasicObject is an object.
BasicObject is an instance of the Class class
BasicObject's superclass is nil.
nil is an object.
nil is an instance of the NilClass class
nil has no superclass, because it is not a class.
だから、最後に:
Class is an object.
Class is an instance of the Class class. # this is probably the most important part.
Class's superclass is Module # 2nd most important part
Module is an object
Module is an instance of the Class class. # 3rd
Module's superclass is Object # 4th
# everything below here is in the above examples.
Object is an object.
Object is an instance of the Class class.
Object's superclass is BasicObject.
BasicObject is an object.
BasicObject is an instance of the Class class
BasicObject's superclass is nil.
nil is an object.
nil is an instance of the NilClass class
nil has no superclass, because it is not a class.
表形式:
そして、それがすべて正しいことを確認したい場合は、Ruby で実行するだけです。
"Nathan".is_a?(BasicObject) # => true "Nathan" is an object.
"Nathan".class #=> String "Nathan" is an instance of the String class.
"Nathan".is_a?(Class) #=> false "Nathan" is not a class.
"Nathan".superclass # NoMethodError "Nathan" has no superclass, because "Nathan" is not a class.
String.is_a?(BasicObject) #=> true String is an object.
String.class #=> Class String is an instance of the Class class.
String.superclass #=> Object String's superclass is Object.
Object.is_a?(BasicObject) #=> true Object is an object.
Object.class #=> Class Object is an instance of the Class class.
Object.superclass #=> BasicObject Object's superclass is BasicObject.
BasicObject.is_a?(BasicObject) #=> true BasicObject is an object.
BasicObject.class #=> Class BasicObject is an instance of the Class class
BasicObject.superclass #=> nil BasicObject's superclass is nil.
nil.is_a?(BasicObject) #=> true nil is an object.
nil.class #=> NilClass nil is an instance of the NilClass class
nil.superclass # NoMethodError nil has no superclass, because it is not a class.
そしてクラスから始めます:
Class.is_a?(BasicObject) #=> true Class is an object.
Class.class #=> Class Class is an instance of the Class class. # this is probably the most important part.
Class.superclass #=> Module Class's superclass is Module # 2nd most important part
Module.is_a?(BasicObject) #=> true Module is an object
Module.class #=> Class Module is an instance of the Class class. # 3rd
Module.superclass #=> Object Module's superclass is Object # 4th
Object.is_a?(BasicObject) #=> true Object is an object.
Object.class #=> Class Object is an instance of the Class class.
Object.superclass #=> BasicObject Object's superclass is BasicObject.
BasicObject.is_a?(BasicObject) #=> true BasicObject is an object.
BasicObject.class #=> Class BasicObject is an instance of the Class class
BasicObject.superclass #=> nil BasicObject's superclass is nil.
nil.is_a?(BasicObject) #=> true nil is an object.
nil.class #=> NilClass nil is an instance of the NilClass class
nil.superclass # NoMethodError nil has no superclass, because it is not a class.