The best Ruby module (Module) Tutorial In 2024, In this tutorial you can learn Rubyrequire statement,Rubyinclude statement,Ruby in Mixins,
Module (Module) is a way to methods, classes, and constants combined manner. Module (Module) provides you with two benefits.
Module (Module) defines a namespace, the equivalent of a sandbox, your methods and constants are not constants and methods of conflict elsewhere in it.
Module similar class, but with a different look:
module Identifier statement1 statement2 /en/en/en/en/en. end
Module named constants and class constants similarly named, beginning with a capital letter. Method definition looks like: similar modular approach to define the class method definitions.
Through the class methods, you can place the module name and a dot in front of the class name of the method to call the modular approach, you can use the module name and two colons to reference a constant.
#!/usr/bin/ruby # 定义在 trig.rb 文件中的模块 module Trig PI = 3.141592654 def Trig.sin(x) # /en end def Trig.cos(x) # /en end end
We can define multiple functions with the same name but different functional modules:
#!/usr/bin/ruby # 定义在 moral.rb 文件中的模块 module Moral VERY_BAD = 0 BAD = 1 def Moral.sin(badness) # /en. end end
Like class methods, when you define a method in a module, you can specify the module name followed by a dot after dot followed by the method name.
require statement is similar to C and C ++, and include statements in the Java import statement. If a third-party program you want to use any of the modules have been defined, you can simply use theRubyrequire statement to load module file:
require filename
Here, the file extension.rb not required.
$ LOAD_PATH << '.' require 'trig.rb' require 'moral' y = Trig.sin (Trig :: PI / 4) wrongdoing = Moral.sin (Moral :: VERY_BAD)
Here, we use$ LOAD_PATH << '.' Let Ruby know that we must search for the referenced file in the current directory.If you do not want to use $ LOAD_PATH, you can userequire_relative to reference a file from a relative directory.
Note: Here, the file contains the same function name.So, this will lead to obfuscation when referring to the calling program but modules avoid this code obfuscation, and we can use the name of the module to call the appropriate function.
You can embed a module in a class. In order to embed in the class module, you can useincludestatements in your class:
include modulename
If the module is defined in a separate file, so before you need to use embedded modulerequirestatements reference the file.
Assuming the following modules written insupport.rbfile.
module Week FIRST_DAY = "Sunday" def Week.weeks_in_month puts "You have four weeks in a month" end def Week.weeks_in_year puts "You have 52 weeks in a year" end end
You can now reference the module in the class, as follows:
#!/usr/bin/ruby $LOAD_PATH << '.' require "support" class Decade include Week no_of_yrs=10 def no_of_months puts Week::FIRST_DAY number=10*12 puts number end end d1=Decade.new puts Week::FIRST_DAY Week.weeks_in_month Week.weeks_in_year d1.no_of_months
This produces the following results:
Sunday You have four weeks in a month You have 52 weeks in a year Sunday 120
Before reading this section, you will need a preliminary understanding of object-oriented concepts.
When a class can be more than one parent class when the feature class inherits from, the class is displayed as multiple inheritance.
Ruby does not directly support multiple inheritance, but Ruby's module (Module) has another fantastic features. It virtually eliminates the need for multiple inheritance, there is provided an apparatus calleda mixin.
Ruby does not really achieve multiple inheritance mechanism, but adopted as mixin technology as a substitute. The modules include the class definition, the module approach to mix into the class.
Let's look at the following sample code, in-depth understanding of mixin:
module A def a1 end def a2 end end module B def b1 end def b2 end end class Sample include A include B def s1 end end samp=Sample.new samp.a1 samp.a2 samp.b1 samp.b2 samp.s1