Ruby basics 6

Source: Internet
Author: User
Module

Similar modules also have class method and instance method.
The instance object cannot be generated if the module does not have a new one.
The class method is called a module method in the module and can be called directly.

Module Foo def self. Hello puts 'Hello world! 'End def Foo. Dear # The self in the global scope of the module has not changed, that is, module; puts 'Dear .. 'end num = 100end




Foo. Hello # => 'Hello world! 'Call module method module name. Method Name
Foo. Dear # => 'ar .. 'Call the module method module name. Method Name
Foo: num # => 100 reference a constant using the module name and two colons.


For the instance method in the module, this method cannot be called directly, and Mixin must be included in a class.
There are two main forms:
One is include, and the method will be added to the instance method.
One is extend, and the method will be added to the class method.

module Mdef self.m_funputs ‘m fun‘enddef instance_funputs ‘instance fun‘endNUM = 100endM.m_funM::m_funputs M::NUMputs ‘-----------------‘class Ainclude Mend#A.m_fun#A.instance_fun#A.new.m_funA.new.instance_funputs ‘-----------------‘class Bextend Mend#B.m_funB.instance_fun#B.new.m_fun#B.new.instance_fun


Some Summary of require, load, and include are all methods in the kernel module. Their differences are as follows:


Require, load is used to include files, and include is used to include modules.


Require is loaded once, and load can be loaded multiple times.


Require can load the ruby code file without a suffix. The suffix must be added when loading the code file.


In general, require is used to load library files, while load is used to load configuration files.

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.