w3resource

Ruby Modules and Mixins

Modules

Modules are a way of grouping together methods, classes, and constants. There are two major benefits in modules :

  • Modules provide a namespace and prevent name clashes.
  • Modules implement the mixin facility.

Namespace : A namespace can be used to organize code by package or functionality that separates common names from interference by other packages. For example, the IRB namespace provides functionality for irb that prevents a collision for the common name "Context".

Mix-in functionality allows sharing common methods across multiple classes or modules. Ruby comes with the Enumerable mix-in module which provides many enumeration methods based on the each method and Comparable allows comparison of objects based on the <=> comparison method.

Module constants are named just like class constants, with an initial uppercase letter.

Method definitions look similar to class methods. Module methods are prefixed with the name of the module similar to class methods are prefixed with the name of the class.

Create a module:

A module is created using the module keyword. Here is the syntax :

module ModuleName
   statement1
   statement2
   ...........
end

Example:

module DecimalCode 
  RED = "rgb(255,0,0)" 
  GREEN = "rgb(0,128,0)" 
  
  def  code 
  return "Red : Decimal code #{RED}." 
  end 
  
  def DecimalCode.code 
   return "Green: Decimal code #{GREEN}. " 
   end 
  
  end
  
  puts(DecimalCode::RED)
  
  puts( DecimalCode.code )

Output:

rgb(255,0,0)
Green: Decimal code rgb(0,128,0).

In the above example we have accessed the module constants just as we would access class constants using the :: scope resolution operator (puts(DecimalCode::RED)) and accessed the module methods using dot notation - that is, specifying the module name followed by a period and the method name ( puts( DecimalCode.code )).

A module may be reopened any number of times to add, change or remove functionality:

module ModuleName
  def my_method
  end
end

module ModuleName
  alias my_alias my_method
end

module ModuleName
  remove_method :my_method
end

Nesting:

Modules may be nested. Many packages create a single outermost module (or class) to provide a namespace for their functionality.

module Outer
  module Inner
  end
end

You may also define inner modules using :: provided the outer modules (or classes) are already defined:

module Outer::Inner::GrandChild 
end 

Note that this will raise a NameError if Outer and Outer::Inner are not already defined.

Mixing in Modules:

An object can access the instance methods of a module by including that module using the include method which is called "Mixing in Modules". See the following example :


module Class5
  def c5
    puts 'Number of students in Class V : 60'
  end
end
module Class6
  def c6
    puts 'Number of students in Class VI : 65'
  end
end
module Class7
  def c7
    puts 'Number of students in Class VI : 62'
  end
end
class Student
  include Class5
  include Class6
  include Class7
  def display
    puts 'Three modules have included.'
  end
end
object=Student.new
object.display
object.c5
object.c6
object.c7

Outputs:

Three modules have included.
Number of students in Class V : 60
Number of students in Class VI : 65
Number of students in Class VI : 62

Previous: Ruby Classes
Next: Ruby Tutorial



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/ruby/ruby-modules-and-mixins.php