home PYTHONJAVA
 

Ruby Module

Modules are a way of combining methods, classes, and constants. Modules offer you two benefits.

    The
  • module provides an namespace and avoids name collisions.
  • The
  • module implements the mixin device.

Modules define a namespace, equivalent to a sandbox, where your methods and constants don't conflict with method constants elsewhere.

The module is similar to the class, but there is a difference:

  • Module cannot be instantiated
  • Module has no subclasses
  • Modules can only be defined by another module

grammar

module Identifier statement1 statement2 ........... end

Module constant naming is similar to class constant naming, starting with an uppercase letter. Method definitions look similar: module method definitions are similar to class method definitions.

Through the class method, you can call the module method by placing the module name and a dot before the class method name. You can use the module name and two colons to reference a constant.

Instance

#!/usr/bin/ruby # a module defined in the trig.rb file module Trig PI = 3.141592654 def Trig.sin(x) # .. end def Trig.cos(x) # .. end end

We can define multiple modules with the same function name but different functions:

Instance

#!/usr/bin/ruby # Defined in moral.rb Module in the file module Moral VERY_BAD = 0 BAD = 1 def Moral.sin(badness) # ... end end

Just like the class method, when you define a method in a module, you can specify that the module name is followed by a dot followed by the method name.

Ruby require statement

The

require statement is similar to the include statement in C and C++ and the import statement in Java. If a third-party program wants to use any of the defined modules, you can simply load the module file using the Ruby require statement:

grammar

grammar

require filename

The file extension .rb is not required here.

Instance

$LOAD_PATH << '.' require 'trig.rb' require 'moral' y = Trig.sin(Trig::PI/4) wrongdoing = Moral.sin(Moral::VERY_BAD)

Here, we use $LOAD_PATH << '.' to let Ruby know that the current directory must be searched for the referenced file. If you don't want to use $LOAD_PATH, you can use require_relative to reference a file from a relative directory.

Note: Here, the file contains the same function name. So, this leads to code ambiguity when referring to the calling program, but the module avoids this code ambiguity, and we can call the appropriate function with the name of the module.

Ruby include statement

You can embed modules in your class. To embed a module in a class, you can use the include statement in your class:

Syntax

include modulename

If the module is defined in a separate file, you need to reference the file with the require statement before embedding the module.

Instance

Assume the following modules are written 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" end end

Now, you can reference the module in a class as follows:

Instance

#!/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 will produce the following results:

Sunday
You have four weeks in a month
You have 52 weeks in a year
Sunday
120

Mixins in Ruby

Before reading this section, you need to get a glimpse of object-oriented concepts.

When a class can inherit the properties of a class from multiple parent classes, the class appears as multiple inheritance.

Ruby doesn't directly support multiple inheritance, but Ruby's module has another magical feature. It virtually eliminates the need for multiple inheritance and provides a device called mixin.

Ruby doesn't really implement multiple inheritance mechanisms, but instead uses mixin technology as a replacement. The module is included in the class definition, and the methods in the module are mixed into the class.

Let’s take a look at the sample code below to learn more about mixin:

Instance

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
  • Module A consists of methods a1 and a2.
  • Module B consists of methods b1 and b2.
  • Class Sample contains modules A and B.
  • Class Sample has access to all four methods, a1, a2, b1, and b2.
  • So, you can see that the class Sample inherits two modules, you can say that the class Sample uses multiple inheritance or mixin .






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.