Introduce the knowledge of modules and mixed types in Ruby and ruby.

Source: Internet
Author: User

Introduce the knowledge of modules and mixed types in Ruby and ruby.

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:

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:

#!/usr/bin/ruby# Module defined in trig.rb filemodule Trig  PI = 3.141592654  def Trig.sin(x)  # ..  end  def Trig.cos(x)  # ..  endend

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

#!/usr/bin/ruby# Module defined in moral.rb filemodule Moral  VERY_BAD = 0  BAD = 1  def Moral.sin(badness)  # ...  endend

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:

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.

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"  endend

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

#!/usr/bin/rubyrequire "support"class Decadeinclude Week  no_of_yrs=10  def no_of_months   puts Week::FIRST_DAY   number=10*12   puts number  endendd1=Decade.newputs Week::FIRST_DAYWeek.weeks_in_monthWeek.weeks_in_yeard1.no_of_months

This produces the following results:

SundayYou have four weeks in a monthYou have 52 weeks in a yearSunday120

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:

module A  def a1  end  def a2  endendmodule B  def b1  end  def b2  endendclass Sampleinclude Ainclude B  def s1  endendsamp=Sample.newsamp.a1samp.a2samp.b1samp.b2samp.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.

Related Article

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.