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.