This is a personal memo.
Class
which refers to the class (type) of the receiver and superclass
which confirms the parent class
Also about Basic Object
.
1 .class
You can easily get the receiver class (type) by using the .class
method.
python
x = 100
x.class
=> Integer
python
str = "text"
str.class
=> String
python
obj = {:a=>1, :b=>2, :c=>3}
obj.class
=> Hash
python
arr = [1, 2, 3]
arr.class
=> Array
python
nil.class
=> NilClass
python
(1..4).class
=> Range
python
(1.23).class
=> Float
python
Class.class
=> Class
Class
is an unnamed class. Called a metaclass.
The class created with the class class name end
has this Class
.
python
class TestClass
def hello
msg = "test class"
p "hello #{msg}"
end
end
TestClass.class
=> Class
python
Kernel.class
=> Module
Kernel
is a module that defines methods that can be referenced by all classes.
python
module TestModule
def helloModule
p "hello Module"
end
end
TestModule.class
=> Module
It cannot be used for elements such as numbers, strings, arrays, hashes, nil, and modules.
It can be used for the type obtained by the class
method.
The parent class is called the superclass.
python
class AAA
end
#Inherit class AAA
class BBB < AAA
end
When the class is inherited as described above, ** the parent class AAA becomes the superclass **.
BBB is called a subclass.
・ Class subclass name <superclass name
python
100.class
=> Integer
100.class.superclass
=> Numeric
100.class.superclass.superclass
=> Object
100.class.superclass.superclass.superclass
=> BasicObject
100.class.superclass.superclass.superclass.superclass
=> nil
Integer's superclass is Numeric. Numeric's superclass is Object.
python
#Error when used for numbers
100.superclass
NoMethodError (undefined method `superclass' for 100:Integer)
python
class Aaa
end
Aaa.class
=> Class
Aaa.superclass
=> Object
Object returns the superclass of Class.
python
class Aaa
end
class Bbb < Aaa
end
BBB.superclass
=> Aaa
You cannot use superclass for module.
python
module Xxx
end
Xxx.class
=> Module
Xxx.superclass
=> NoMethodError (undefined method 'superclass' for Xxx:Module)
Hierarchical structure of classes
BasicObject
↓
Object
↓
String, Numeric, Module, Class,,,
↓
Integer, Floot
In Ruby, all data is called an Object, and underneath it is a string of strings and a Numeric of numbers.
The higher you go, the more abstract and simple the definition becomes. BasciObject is the class at that time.
python
BasicObject.superclass
=> nil
Recommended Posts