Modules in ruby

Source: Internet
Author: User

1. Differences with classes:

A. No instance is available.

B. No subclass exists.

C. defined by module... end.

2. There are two main roles:

A. As a collection of methods or constants, such as math.

IRB (main): 001: 0> math. SQRT (4)
=> 2.0
IRB (main): 002: 0> Math: pi
=> 3.14159265358979
IRB (main): 003: 0>

It should be noted that: operators are constants used to reference modules or classes.

B. Used for Mix-in.

We know that OO contains single inheritance, such as Java, multiple inheritance, such as C ++ or Python, and Ruby is also single inheritance. However, Min-in can also be used to implement multi-inheritance, such as interfaces in Java. The so-called mix-in is to introduce constants or methods in a module into another module or class through include.

Enumerable is an example of Mixin in the standard library. By mixing it into your class, if your each method can return an object, you will also have sort, find and other functions.

Differences between Mixin and multi-inheritance:

    1. The module does not generate instances.
    2. The module keeps the class hierarchy tree instead of the mesh.
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.