Introduction to knowledge about modules and mixed types in Ruby _ruby topics

Source: Internet
Author: User
Tags constant inheritance mixed sin

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.
Grammar:

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.
Example:

#!/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:

#!/usr/bin/ruby

# module defined in moral.rb file

Module moral
  Very_bad = 0 Bad
  = 1
  def moral.si N (badness)
  #
  ... End End


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:
Grammar:

Require filename

Here, it is not required. rb file name extension.
For example:

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:
Grammar:

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.
Example:

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

Module Week
  first_day = "Sunday"
  def Week.weeks_in_month puts "you have four weeks into
   a month"
  end
  de F week.weeks_in_year
   puts "you have weeks in a year" end


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

#!/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
d1=decade.new
puts :: First_day
week.weeks_in_month
week.weeks_in_year
d1.no_of_months

This will produce the following results:

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:

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=sample.new
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

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.