ruby-Modules and Classes

Source: Internet
Author: User

First of all, look at their relationship.

    1. IRB (main):100:0> String.class
    2. = = Class
    3. IRB (main):101:0> String.class.superclass
    4. = Module
    5. IRB (main):102:0> String.class.superclass.superclass
    6. = = Object

Class < Module < Object

1. Class can only be inherited, but may contain more than one module

Module AA
def aa.prints1 (str)
Print "A:" +str
End
End


Module BB
def bb.prints2 (str)
Print "B:" +str
End
End

Class CC
Include AA
Aa.prints1 ("AA")
Include BB
Bb.prints2 ("BB")
End

2, the module is similar to the C # interface, including some common interface methods, can not be instantiated

Class to invoke the module in two ways

A.RB the module name before the method. The method name (AA.PRINTS1), which can be called directly in its reference class Aa.prints1

Module AA
def aa.prints1 (str)
Print "A:" +str
End
End

B.RB If you do not add a module name, you need to include the include BB in its reference class, which is equivalent to inheriting the method, and then you can call the PRINTS2 directly.

Module BB
def prints2 (str)
Print "B:" +str
End
End

ruby-Modules and Classes

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.