Easy Tutorial
❮ Ruby File Methods Ruby Block ❯

Ruby Module

A Module is a way to group methods, classes, and constants together. Modules provide two main benefits:

A Module defines a namespace, acting like a sandbox where your methods and constants do not conflict with those from other places.

Modules are similar to classes but have the following differences:

Syntax

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

Module constant names are similar to class constant names, starting with a capital letter. Method definitions look similar too: module method definitions are akin to class method definitions.

You can call module methods by prefixing the module name and a dot to the method name, and you can reference a constant using the module name and two colons.

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 multiple modules with the same function names but different functionalities:

Example

#!/usr/bin/ruby

# Module defined in moral.rb file

module Moral
   VERY_BAD = 0
   BAD = 1
   def Moral.sin(badness)
   # ...
   end
end

Like class methods, when you define a method in a module, you can specify the module name followed by a dot and then the method name.

Ruby require Statement

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

Syntax

require filename

Here, the file extension .rb is not necessary.

Example

$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 it must search for referenced files in the current directory. If you do not want to use $LOAD_PATH, you can use require_relative to reference files from a relative directory.

Note: Here, files contain the same function names. This can cause code ambiguity when referenced in the calling program, but modules avoid this ambiguity, and we can call the appropriate function using the module name.

Ruby include Statement

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

Syntax

include modulename

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

Example

Suppose the following module is written in 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 this module in a class as follows:

Example

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

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 have a basic understanding of object-oriented concepts.

When a class can inherit features from more than one parent class, the class exhibits multiple inheritance.

Ruby does not directly support multiple inheritance, but Ruby modules have another wonderful capability. They almost eliminate the need for multiple inheritance, providing a feature called mixin.

Ruby does not implement multiple inheritance through classes but uses a technique called mixin. By including modules in class definitions, the methods in those modules are mixed into the class.

Let's look at the following example code to understand mixins better:

Example

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
❮ Ruby File Methods Ruby Block ❯