Ruby Study Notes-Module

Source: Internet
Author: User

Module:

The module definition is similar to the class, and the module keyword is used. However, a module cannot be instantiated or quilt-type. modules are independent and a module is an instance of a module class. The two most common functions of a module are as the life space and Mixin ).

In a module, you can define instance variables, instance methods, class variables, class methods, and attributes, and define classes and modules in the module. You can also define modules in the class.

The instance members in the access module must include modules in the class, and then instantiate the class to access the instance members of the module.

Module firstmodule

Def go

Puts "go home"

End

 

Def self. show # or FirstModule. show

Puts "It's a red car ."

End

End

FirstModule. show # It's a red car.

 

Class Car

Include FirstModule # insert in class

End

Car = Car. new

Car. go # Go home

 

A) method calls in the module:

Module Test

Def self. function

Puts "this is function test"

End

End

Call: Test. function

 

Module Test2

Class TestClass

Def performance

Puts "this is performance test"

End

End

End

Call: Test2: TestClass. new. performance

 

B) module constant call

Module TestConst

PI = 1, 3.14

End

Call: TestConst: PI

 

C) the module is used in the Command Space.(Namespace): prevents command conflicts.

D) Modules Used for mixing(Mixins ):

The purpose is to implement multi-inheritance in the class to mix modules and inherit from the base class very similar, for instance object can call instance members, as mentioned above. The class can contain multiple modules, but the class cannot inherit from multiple classes. When you need to add many additional functions to the instance of the class and do not want to put all these functions in the base class, you can use modules to organize code.

 

E) load and request module

I. Sometimes different classes or modules are stored in different files. When using these files, you need to use load and require to load these files. When using the modules and objects in the file, you also need to use the include and extend methods.

Ii. Differences between require and load:

    1. The load method parameter must contain the complete file name of the file extension. require only needs to pass the name to the database, and does not need a suffix like the file name;
    2. Load loads a file multiple times, while require loads the file only once at most;
    3. The require method can load ruby source files and source files written in other languages. The load method is used to copy and paste the files;

Iii. include method:

This method is mainly used to insert a module into a class or other modules. Introduce a module into the class definition so that the methods in the module become the instance methods of the class. The methods in this module are called in the form of functions in the class or module that is introduced, include does not simply copy the instance method of the module to the class, but creates a class to the reference of the module ., For example;

Module, file name: TestModule

Module Foo

Def hello

Puts "This is my first module in Ruby! "

End

End

 

The file name of another class is: Test # Test class and TestModule are in the same path.

# Require "D:/cucumber/Ruby/Module" # (file loading) absolute path

Require "../Ruby/Module" # (Load file) Relative Path

 

Class TestOne

Include Foo # modules

Def hi

Puts "This is a class"

End

End

A = TestOne. new

A. hi

A. hello

 

Iv. Extend method:

Extend is used to introduce a module into an object (object, or instance). This class also provides the methods of this module, such:

Module firstmodule

Def hellomod

Puts "this is a module ."

End

End

 

Class firstclass

Def helloclass

Puts "this is a class ."

End

End

C = FirstClass. new

C. helloClass # This is a class.

C. extend (FirstModule)

C. helloMod # This is a module.

 

V. load path:

The Ruby loading path is an array and can be accessed using the global variable $ LOAD_PATH or $. Each element of the array is a directory history. ruby searches for loaded files in these directories, and the preceding directory is preferred to the subsequent directory. puts $: Output path can be used; the Ruby program can also modify the loading path by modifying the content of the $ LOAD_PATH ($ :) array. For example:

Puts $:

$: <".../..." # Modify the value of the loading path

Puts "@@@@@@@"

Puts $:

Of course, we can use absolute paths for load or require to completely bypass the load path;

Related Article

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.