Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby Module

Ruby Module

A Module is a way of grouping methods, classes, and constants together. A Module provides you with two major benefits.

A Module defines a namespace, equivalent to a sandbox, in which your methods and constants will not conflict with methods and constants elsewhere.

Modules are similar to classes, but with the following differences:

Syntax

module

Identifier

statement1

statement2

...........

end

Module constant naming is similar to class constant naming, starting with an uppercase letter. Method definitions look similar too: module method definitions are similar to class method definitions.

With class methods, you can call a module method by placing the module name and a dot before the class method name, and you can reference a constant using the module name and two colons.

Example

!/usr/bin/ruby

Module defined in the 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 functionality:

Example

!/usr/bin/ruby

Module defined in the moral.rb file

module

Moral

VERY_BAD

=

0

BAD

=

1

def

Moral

.

sin

(

badness

)

...

end

end

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

Ruby require Statement

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

Syntax

Syntax

require

filename

Here, the file extension .rb is not required.

Example

$LOAD_PATH

<<

'

.

'

require

'

trig.rb

'

require

'

moral

'

y

=

Trig

.

sin

(

Trig

::

PI

/

  1. wrongdoing = Moral.sin(Moral::VERY_BAD)

Here, we use $LOAD_PATH << '.' to let Ruby know that it must search the current directory for the referenced file. If you don't want to use $LOAD_PATH, then you can use require_relative to reference a file from a relative directory.

**Note:** Here, the files contain the same function names. So this can cause code ambiguity when referencing the calling program, but modules avoid this ambiguity, and we can use the module's name to call the appropriate function.

Ruby include Statement

You can embed a module in a class. To embed a module in a class, you can use in the class include Statement:

Syntax

include

modulename

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

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

When a class can inherit features from multiple parent classes, the class exhibits multiple inheritance.

Ruby does not directly support multiple inheritance, but Ruby's Module has another magical feature. It almost eliminates the need for multiple inheritance, providing a mechanism called mixin .

Ruby does not actually implement a multiple inheritance mechanism; instead it adopts the so-called mixin technique as a substitute. By including a module into a class definition, the methods in the module are mixed into the class.

Let us look at the following example code to gain a deeper understanding of mixins:

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