These four methods are still very useful, it is more easier to mix.
1. Require (astring) -> True Or False
Ruby Attempts to load a library named astring, returns true if it is successful, otherwise returns false. If a given value is not an absolute path, then it will be found in $:. If a given name is with .RB, it is loaded as a source file; if the extension is .so, .o, .dll, etc. (according to different platforms), Ruby will be loaded into the extension; otherwise, Ruby will Automatically attempt to add .rb, .so, .dll, etc. after a given file name. The loaded library will be placed in array $ ", it is already in $" will not be loaded. such as:
Require "My-library.rb" Require "DB-Driver"
2. Load (AfileName, Wrap = False) -> True
Load and execute the AfileName file, the file search method is the above Require. Wrap is an optional parameter, default is false, if set to true, this file will run in anonymous module, including the caller's namespace. The local variables in any AfileName are not available in the environment loaded.
3. Include
INCLUDE is mainly used to insert a module into a class or other module. The method of this module is called in the form of a function in the introduction of its class or module (no receiver). This instruction will perform the Module.Append_features method when running.
4. Extend
EXTEND is used to introduce a module in an object (Object, or is instance), which is therefore also have this module.
Module Mod Def Hello2 "Hello from MOD.N" End Class Klass Def Hello "Hello from klass.n" End K = klass.new k.hello # "Hello from klass.n" k.hello2 # nomethodError: undefined Method `Hello2 '... k.extend (MOD) # <0x2e4c530>
K.hello # "Hello from mod.n"