它可能根本不是一个悖论,但从新手的角度来看,它看起来确实如此。
> Class.superclass
=> Module
> Class.superclass.class
=> Class
> Class.superclass.class.superclass
=> Module
所以类的父级是模块,但模块是一个类?
我怎样才能理解这一点?
答案 0 :(得分:73)
TL; DR:模块 类的超类。模块是 类的实例。
<小时/> 让我试着更清楚地解释一下。请原谅我的手写图纸 - 我没有任何精美的绘图软件。
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的一个实例。 String是Class等的实例......
意识到Fixnum是Class的一个实例,就像“Nathan”是String的一个实例一样。就像“John”是String的一个实例一样,Float只是Class的一个实例。每个类只是一个Class的实例,甚至是Class本身!
每当你在app中编写一个新类时,你只是实例化一个类为Class的新对象,就像Hash.new实例化一个新的Hash,或者“Nathan”实例化一个新的String。
# 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
将无效。
因此,回头看图,您的问题可以直接回答:
所以类的父级是模块,但模块是一个类?
您可以从顶部图片中看到:模块是 类的超类。
从下图:模块是 类的实例。
答案 1 :(得分:15)
在第二个示例Class.superclass.class中,您正在调用Module.class。模块是指一个类,即Module类。
AnyClass.superclass.class
将返回Class,BasicObject.superclass.class
类和实例之间的区别很重要。 BasicObject是一个类。这是一个扩展为nil的类,这是一种说它没有超类的奇特方式。它是树的根。一切都是一个对象,这是说一切都是某个类的实例的另一种方式。
字符串示例
"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.
Fixnum示例
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开始:
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.