Ruby Module: A Major OOPs Feature of the Ruby Programming Language
This major programming language was invented in 1993 by Yukihiro Matsumoto. It has features resembling those of Smalltalk, Perl and Python. This language is open source and freely available on the web. However, it is subject to a license. It is a true object oriented language which can be embedded into Hypertext Markup Language. Ruby is a true object oriented language as well as a server side scripting language. It is a interpreted language which has a clean and easy syntax. This language is easy to learn and incorporates a rich set of built-in functions. The features of this language make it ideal for developing Internet and intranet applications. The advantage of this language is that the programs written in it are scalable and easy to maintain. Ruby can easily be installed and run on Windows and POSIX environments. It provides support for major GUI tools such as Tcl/Tk, GTK and OPenGL. Popular Ruby editors include VIM (Vi IMproved). This is a simple text editor available on most Unix machines and now on Windows too. There is also Ruby Win which is a Ruby Integrated Development Environment for Windows. Also we have the Ruby Development Environment for Windows.
Ruby Module is an important part of the Ruby programming language. It supports multiple inheritance indirectly and is a major object-oriented feature of the language. Today we walk you through this intermediate level tutorial on Ruby. We assume that you know the basics of Ruby. Else we suggest that you go first take this beginners course on Ruby programming language. If you just need a refresher, a quick read through this Ruby tutorial should suffice.
Introduction to Object-Oriented Programming Concepts (OOPs)
Before object-oriented programming came into existence, there were structured programming languages like C. The problem was when the programs became too big it was too difficult to understand and debug. Also the structured programming languages kept data and functions separate. However, in the actual world entities have both data and functions. Object Oriented programming was invented to model the real world. It sees the world in terms of objects which have properties (data) and behavior (functions). For example, a car is an object which has certain properties such as color, weight, make and so on. It also has certain functionalities such as start car, accelerate, brake and so on.
We look at the concepts which form the basis of Object-Oriented programming.
- Object– when you bundle both the data and functions that operate on that data into a single unit it is called an object.
- Class– It specifies the blueprint for an object. It tells you what is the class name and what are the different properties and methods of the class. When you instantiate a class an object of that class is formed.
- Abstraction– Only certain data is exposed to the world and the others are hidden. For example, you can see the screen of the television but you do not know its internal functions.
- Encapsulation– It is the bundling of the data and functions that operate on the data in the same place.
- Inheritance– Just like children inherit looks and behavior from their parents, derived classes inherit properties and functions of their base classes or parent classes. This prevents rewriting of data and function code. it is useful as it provides code reusability.
Modules group together methods, classes and constants. There are two main advantages of modules
- They provide a namespace which prevents name clashes.
- Secondly modules implement the mixin feature.
The syntax of Ruby module is as follows
Module constants are named similar to the way that class constants are. They have an initial uppercase letter. The methods of module are defined in the same way as class methods. To call a module method, precede its name with the module name and a period. To reference a constant precede its name with the module name and two colons.
Example 1: Program to Implement Ruby Module
PI = 3.141592654
def Trig.sin(y) ............... end
def Trig.cot(y) ................. end
In this program the first line gives the path of the Ruby executable. The keyword module declares the name to be of type module. To access methods, we write modulename.method. Secondly, to access constants, we write modulename:: constant name. You can learn more about Ruby methods and how to access them in this Ruby course.
Example 2: Another Program which Defines a Module
BAD = 0
GOOD = 1
def Moral.sin(quality) ........................ end
Ruby Require Statement
The Ruby require statement is like the include statement of C and C++ programming languages and also the import statement of Java. If a certain program wants to use a defined module, it can load it by using the Ruby require statement.
The syntax looks like this:
Note that .rb extension is not required along with the file name.
To advance your knowledge on Ruby, we suggest that you go through this tutorial on Ruby
Example 3: Program Using Require Statement
$LOAD_PATH << '.'
z = Trig.cot(Trig::PI/4)
new_behave = Moral.sin(Moral::GOOD)
In this program, $LOAD_PATH << ‘.’ tells Ruby that files must be searched in the current directory. If you do not wish to use this command, you can use require_relative command which includes files from a relative directory.
Note that both the files contain identical function names. Hence it will cause code ambiguity when included in calling programs. Modules avoid this code ambiguity and it is possible to call the right function using the appropriate module name.
Ruby Include Statement
This statement embeds a module in a class. The syntax is as follows:
Note that if you have defined the module in a separate file, then you need to include that file using “require” statement. This must be done before embedding a module in a class. To learn more about how to use this, you can take this practical training on Ruby.
Consider the following example. We will be calling this module created in this example in the example which follows it.
FIRST_DAY1 = "Monday"
puts "There are four weeks in a month"
puts "There are 52 weeks in a year"
In this program, we have declared the module name as Week1. In this module, we have declared a variable and two methods, which we will be using in the subsequent program. The name of the file containing this program is called Calendar1.rb.
$LOAD_PATH << '.'
require " Calendar1"
In this program, the first line gives the path of the Ruby executable. The $LOAD_PATH << ‘.’ command searches for the file in the current directory. The “require Calendar1” statement loads the Calendar1 file. Then the class in this program includes the Week1 module. So in a way the class inherits the methods and constants of the module. This is a kind of inheritance. In order to print a constant of Week1 we precede the constant with the name of the container module (Week1) and “::” . Also to use the methods we precede the method name with the container module and period operator. So we see that module is very similar to a class in terms of calling its constants and methods in a Ruby program. This course can help better understand the differences between class and module in Ruby programming.
The following result will be the output:
There are four weeks in a month
There are 52 weeks in a year
Mixin in Ruby
When a class inherits properties and functions from multiple classes, its known as multiple inheritance. Multiple inheritance is not supported by Ruby directly. However, Mixins which is a major feature of Ruby provides this facility. Take a look at the following code. Note that to understand this code, you should know the basic concepts of OOPs. We suggest that you brush up your basics on OOPs.
Module X has the methods x1 and x2. Module Y has the methods y1 and y2. The class sample includes both the modules. So it can access all the four methods that is x1, x2, y1 and y2. Hence it is clear that the class sample inherits from two modules. Therefore you can say that class sample displays multiple inheritance or a mixin.
Hope this article was fun and informative. Work on the examples and also create your own code to learn more about Ruby modules and mixins. Once you’re ready to move to the next level, this advanced course can help you get a better mastery over Ruby.
Ruby students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.