Say, you want to create a Method to add two numbers and want that add Method to be used by every other programmer, who wants to use it.
And this is where a Module into picture.
A Module can have a variable and a method.
Creating a Module is super simple. All you need to do is, create a file with .rb extension and create your Module there.
Let us explain it with the below example.
module MyAdd def MyAdd.addition(x, y) z = x + y return z end end
$LOAD_PATH << '.' require "add.rb" a = 10 b = 11 c = MyAdd.addition(a,b) puts "The added result is #{c}"
So, in the above code, we have two files add.rb and main.rb.
Let us talk about add.rb first.
So, add.rb is the file, where we declare our Module. We have declared the Module using the keyword module followed by the Module name.
module MyAdd
Just remember, a module is just like a block where all your code lies. And it ends with an end statement.
module MyAdd ... ... end
Now, inside the Module, MyAdd, we have created a Method called addition(x, y), that takes two values as Arguments(i.e. x and y). And returns the added result z.
Just note the method definition must have the Module Name, i.e. MyAdd followed by a . then the Method name (i.e. def MyAdd.addition(x, y)).
def MyAdd.addition(x, y) z = x + y return z end
Then we have saved it in a file and named it add.py.
And that's it. We are done creating a Module.
Our next task would be to use the Module, MyAdd inside the add.py file.
For that let us come to the second file, main.py. Which is also our main application.
And the first thing we do is, use,
$LOAD_PATH << '.'
That loads all the files from the current directory(i.e. add.rb where our Module is defined).
Then we have imported the Module in the file, add.rb using the require keyword.
require "add.rb"
The next thing we have done is, created two variables a and b, and initialised them with 10 and 11.
a = 10 b = 11
Then we try adding them using the addition(x, y) Method, that is defined in the add.rb file's module MyAdd.
So, to invoke the addition(x, y) Method, we have used the dot . operator with the module name MyAdd.
c = MyAdd.addition(a, b)
And the addition Method gets called from the add.rb file's module MyAdd.
And we get the added result returned and printed.
So, to summarise, creating and invoking a Module is a four step process.
module MyAdd def MyAdd.addition(x, y) z = x + y return z end end
$LOAD_PATH << .
require "add.rb"
c = MyAdd.addition(a, b)
So far, we have seen that we can have Methods in a Module. But we can also have variables in a Module. It can be a Integer or a String, an Array or a Hash.
Say for example, we need a Module that will have an Array with three names,
NAMES = ["John", "Sean", "Prakash"]
Well! All the letters in the array variable (i.e. NAMES) are in upper case. And that's put on purpose.
The reason is a variable (Be it a Integer or a String, an Array or a Hash) inside a Module must be a constant. And a constant variable in Ruby, should always be in upper case.
Then we can have an addition() Method that will add two numbers and return the result.
So, we can create a Module in the same way and declare an Array(Well! Array must be a constant) along with a Method.
Let us see in the below example.
module NewModule NAMES = ["John", "Sean", "Prakash"] def NewModule.addition(x, y) z = x + y return z end end
$LOAD_PATH << .
require "newaddMethod.rb"
a = 10
b = 11
c = NewModule.addition(a, b)
puts "The added result is #{c}"
name = NewModule::NAMES[1]
puts "The second name in the List is #{name}"
So, in the above Example, we have created a module named, newaddMethod.py.
module NewModule NAMES = ["John", "Sean", "Prakash"] def NewModule.addition(x, y) z = x + y return z end end
And created a List (i.e. NAMES) containing three names,
NAMES = ["John", "Sean", "Prakash"]
And also the module has a Method def addition(x, y).
def NewModule.addition(x, y) z = x + y return z end
Now, in our main application main.rb, we have created two variables a and b and assigned them with 10 and 11.
a = 10 b = 11
Then as usual, we have called the addition(...) Method using the NewModule module.
c = NewModule.addition(a, b)
Next, we have used the Module name NewModule to access the second name of the List NAMES, using the :: operator.
name = NewModule::NAMES[1]
And as we can see, the second element in the List is, Sean. And we got it printed.
The second name in the List is Sean
Say for example, we need a Module that will have a number, 5 and a String "Tokyo",
And as usual, we can have an addition() Method that will add two numbers and return the result.
So, we can create a Module in the same way and declare an integer and String(Well! integer and String must be a constant) along with a Method.
Let us see in the below example.
module NewModule NUM = 5 STR = "Tokyo" def NewModule.addition(x, y) z = x + y return z end end
$LOAD_PATH << .
require "newaddMethod.rb"
a = 10
b = 11
c = NewModule.addition(a, b)
puts "The added result is #{c}"
numb = NewModule::NUM
name = NewModule::STR
puts "The number in the module is #{numb}"
puts "The String in the Module is #{name}"