(1) The relationship between module and class, module and namespace, devise
#查看一个类的父类, the second description class inherits from the module (the module is the parent class of the Class)
Puts String. Superclass,Class. Superclass,Module. Superclass
#ruby没有命名空间的概念, used to avoid and isolate duplicate variable names and class names, and so on, the function of the Ruby module is namespace
#rails开发时, the Devise library is used to authenticate user information, such as user encryption, automatic update of IP address login time after login, etc.
#Devise是个大模块, it contains a lot of small modules, such as updated modules, landing modules, authentication module
#如下需要用到Devise的login, the direct include comes in.
User
include devise:: Login
(2) mix-in, keyword include, extend
The initial letter must be uppercase if not uppercase (such as class and module), the system error must be constant (because the constant capitalization)
The module name must be capitalized first, or it will be reported as an error.
Module First
A=1
def Greet
P "greet"
End
End
Module Second
B=2
def Self hello
P "Hello"
end
End
class Student
include first
extend first
extend Second
< Span style= "color:navy;" > def Initialize ( no )
@no = no
end
End
< strong>
a = Student .new (1)
a .greet Span style= "Color:gray;" > #这个混入可以使用
Student #这个混入可以使用
Student .hello #extend Second cannot use the module's own self. method, which is a module that can never be mixed with the
#重点: include divides the method into an instance method of the class, extend divides the method of the module into the class method of the class
(14) complement and mix-in of modules (module)