Introduction to modules and mixed types in Ruby

Source: Internet
Author: User

Introduction to modules and mixed types in Ruby

This article mainly introduces the knowledge about modules and hybrid types in Ruby, including polymorphism and inheritance, which are common examples. For more information, see

A module is a combination of methods, classes, and constants. Two main advantages of the module:

The module provides a namespace to avoid name conflicts.

The module implements a hybrid factory.

The module defines a namespace, and a sandbox method and constant can be used freely without worrying about other methods and constants.

Syntax:

?

1

2

3

4

5

Module Identifier

Statement1

Statement2

...........

End

It is like being named as a constant in the class constant module. The first letter is capitalized. The defined methods look very similar. The module-defined methods are like class methods.

Call a module method is the same as a class method. Use the module name and two colons to reference a constant before the module name.

Example:

?

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 = 1, 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)

#...

End

End

The method is the same as the method of the class. When the method defined in a module and the module name is followed by a vertex, the method name is used.

Ruby require statement:

The require statement declares include statements similar to C/C ++ and Java import statements. If a third program needs to use any defined module, it can simply use the Ruby require statement to load the module File:

Syntax:

Require filename

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

For example:

?

1

2

3

4

5

Require 'trig. rb'

Require 'moral'

 

Y = Trig. sin (Trig: PI/4)

Wrongdoing = Moral. sin (Moral: VERY_BAD)

Important: Both files contain the same function name. Therefore, this will lead to ambiguity in the Code and be included in the calling program. However, the module of this Code avoids blurring and we can call the name of the appropriate functional module.

Ruby include statement:

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

Syntax:

Include modulename

If a module is defined in a separate file, it must contain the file and be hidden from the public module before the require statement of a class.

Example:

Write the following modules 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 in a month"

End

Def Week. weeks_in_year

Puts "You have 52 weeks in a year"

End

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

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:

?

1

2

3

4

5

Sunday

You have four weeks in a month

You have 52 weeks in a year

Sunday

120

Mixed types in Ruby:

Before using this section, we assume that you have an object-oriented concept and knowledge.

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

Ruby does not directly inherit from suppoprt, but Ruby's module has another outstanding use. They almost eliminate the need for multi-inheritance and provide a factory called blending.

The hybrid type adds a function class to a wonderful control method. Mix classes in the Code and use its code to interact.

Let's take a look at the following sample code to get a better understanding of mixed types:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

Module

Def a1

End

Def a2

End

End

Module B

Def b1

End

Def b2

End

End

 

Class Sample

Include

Include B

Def s1

End

End

 

Samp = Sample. new

Samp. a1

Samp. a2

Samp. b1

Samp. b2

Samp. s1

Module A includes a1 and a2. Module B includes a method b1 and b2. The class example includes two modules, A and B. The samples can access all four methods, namely a1, a2, b1 or b2. Therefore, we can see that this class inherits from two module samples. Therefore, it can be said that the example of a class shows multiple inheritance or mixing.

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.