Describes the knowledge of modules and mixed types in Ruby

Source: Internet
Author: User
Tags constant inheritance mixed require sin

This article mainly introduces the modules in Ruby and mixed types of knowledge, including common people often say polymorphism and inheritance and other related knowledge, the need for friends can refer to the

Modules are methods, classes, and constants that are grouped together. Module two main benefits:

The module provides a namespace and avoids name collisions.

Module to implement a hybrid factory.

The module defines a namespace in which methods and constants in a sandbox are free to use without fear of stepping onto other methods and constants.



1 2 3 4 5 Module Identifier statement1 statement2 .... end.

Like a constant in a class constant module, the first letter is capitalized. The defined method looks very similar, and the module defines the method as if it were a class method.

Call a module method and a class method, referencing a constant using the module's name and two colons, just before its name.



1 2 3 4 5 6 7 8 9 10 11 12-13 #!/usr/bin/ruby # module defined in trig.rb file Module trig PI = 3.141592654 def trig.sin (x) # ... End def trig.cos (x) # ... End End

We can define a function with the same name, but in different functional modules:


1 2 3 4 5 6 7 8 9 10 11 #!/usr/bin/ruby # module defined in moral.rb file Module moral very_bad = 0 bad = 1 def moral.sin (badness) # E nd

Like the method of a class, when a method defined in a module specifies a point followed by the name of the module, then the name of the method.

Ruby Require statement:

The Require statement declares an include and Java import statement that is similar to C + +. If there is a third program that uses any of the defined modules, it can simply use the module file loaded with the Ruby require statement:


Require filename

Here, it is not required. rb file name extension.

For example:


1 2 3 4 5 Require ' trig.rb ' require ' moral ' y = Trig.sin (trig::P i/4) wrongdoing = Moral.sin (Moral::very_bad)

Important: Here, these two files all contain the same function name. Therefore, this will result in ambiguity in the code, while including the calling program, but the module avoids this code blur and we are able to invoke the appropriate function module name.

Ruby include statement:

Can be embedded in a class module. To embed a module in a class, you can use the include statement in the class:


Include ModuleName

If a module is defined in a separate file, then it needs to include the file that needs to be hidden in the exposed module before the require statement of a class.


Consider the following module written in the Support.rb file.


1 2 3 4 5 6 7 8 9 Module Week first_day = "Sunday" def Week.weeks_in_month puts "you have four weeks into a month" End Def Week.weeks_in_year Puts "You have weeks in a year" end

Now, you can include this module in the following categories:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 #!/usr/bin/ruby require "Support" class decade include Week no_of_yrs=10 def No_of_months puts Week::first_day number=10 *12 puts number end puts Week::first_day week.weeks_in_month week.weeks_in_year d1.no_of_months

This will produce the following results:


1 2 3 4 5 Sunday you have four weeks in a month you have to weeks in a year Sunday 120

Mixed types in Ruby:

Before you pass this section, assume that you have object-oriented concepts and knowledge.

When a class can inherit from multiple parent classes, the class should display multiple inheritance.

Ruby has no direct suppoprt multiple inheritance, but Ruby's module has another wonderful use. They almost eliminate the need for multiple inheritance, providing a factory called mixing.

Blending types give a wonderful control over the way to add functional classes. Mix the classes in your code, and use its code to interact with it.

Let's take a look at the sample code below to get a mixed type understanding:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The 25 26 Module A def A1 end def A2 end-module B def B1 end def B2 End Class Sample include a include B def S1 end samp.a1 samp.a2 samp.b1 samp.b2 samp.s1

Module A includes a method, A1 and A2. Module B includes a method of B1 and B2. The class example includes two modules A and B samples that can access all four methods, namely A1, A2, B1 or B2. Therefore, you can see that this class inherits from two module samples. Therefore, you can say that examples of classes show multiple inheritance or mixing.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.