Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - MODULE


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.


Creating a Module with a Method


Example :


add.rb




module MyAdd

	def MyAdd.addition(x, y)
		z = x + y
		return z
	end
			
end	



main.rb



$LOAD_PATH << '.'

require "add.rb"

a = 10
b = 11
c = MyAdd.addition(a,b)
puts "The added result is #{c}"


Output :



  The added result is 21

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).


Note : For this application to work, place the files 'add.rb' and 'main.rb' in the same folder.

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.

java_Collections

And we get the added result returned and printed.


So, to summarise, creating and invoking a Module is a four step process.

  1. Create a Method (i.e. addition()) inside the module(i.e. MyAdd) and save it in a file named say add.rb.

    And the Module is created.

    add.rb



    module MyAdd
    	def MyAdd.addition(x, y)
    		z = x + y
    		return z
    	end
    end

  2. Next, use the below line to add the file add.rb from the same folder.

    $LOAD_PATH << .

  3. Then, in the calling program, main.rb, import the file using the require keyword that has the Module.

    require "add.rb"

  4. Then call the Method (i.e. addition(...)) using the module name (i.e. MyAdd).

    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.


Creating a Module with Array


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.


Example :


newaddMethod.rb



module NewModule
	NAMES = ["John", "Sean", "Prakash"]

	def NewModule.addition(x, y)
		z = x + y
		return z
	end
end		



main.rb



$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}"


Output :



  The added result is 21
  The second name in the List is Sean

So, in the above Example, we have created a module named, newaddMethod.py.


newaddMethod.rb


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"]
java_Collections


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]
java_Collections


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

Creating a Module with an Integer and String variable


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.


Example :


newaddMethod.rb



module NewModule
	NUM = 5
	STR = "Tokyo"

	def NewModule.addition(x, y)
		z = x + y
		return z
	end
end		



main.rb



$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}"


Output :



  The added result is 21
  The number in the module is 5
  The String in the Module is Tokyo

Note : Just remember the methods in a module is accessed by a dot . and a variable in a module is accessed by '::'.