(14) complement and mix-in of modules (module)

Source: Internet
Author: User

(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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.