Skip to main content

Wuya Tutorial-Ruby - Module Statement

Wuya Tutorial-Ruby - Module Statement

Module syntax

    module Identifier
statement1
statement2
...........
end

Module constants are named like class constants, starting with a capital letter. Method definitions also look similar: module methods are defined just like class methods.

As with class methods, you can call module methods by prefixing the module name with a period, and reference constants with the module name and two colons :: .

    #!/usr/bin/[ruby](/search?q=ruby)

# Module defined in trig.rb file

module Trig
PI=3.141592654
def Trig.sin(x)
# ..
end
def Trig.cos(x)
# ..
end
end

[Wuya Tutorial](/search?q=Wuya Tutorial) can define another module with the same function name but different functions -

    #!/usr/bin/ruby

# Module defined in moral.rb file

module Moral
VERY_BAD=0
BAD=1
def Moral.sin(badness)
# ...
end
end

As with class methods, whenever you define a method in a module, you specify the module name, followed by a dot, and then the method name.

Require statement

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

    require filename

Here, there is no need to give the .rb extension as well as the file name.

    $LOAD_PATH << .

require trig.rb
require moral

y=Trig.sin(Trig::PI/4)
wrongdoing=Moral.sin(Moral::VERY_BAD)

include statement

You can embed modules within classes. To embed a module in a class, use include statement in the class -

    include modulename

If the module is defined in a separate file, you need to include that file using a require statement before embedding the module in a class.

Consider the following module written in the 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 include the module in a class like this:

    #!/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 output -

    Sunday
You have four weeks in a month
You have 52 weeks in a year
Sunday
120

Mixin

Ruby does not directly support multiple inheritance, but there is another great use for Ruby Modules. It provides functions called mixins that almost eliminate problems with multiple inheritance.

Mixins provide you with an excellent controlled way to add functions to your classes. However, their true power is revealed when the code in a mixin starts interacting with the code in the classes that use it.

Let Wuya Tutorial check the following sample code to understand the mixin -

    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

Module A consists of methods a1 and a2. Module B consists of methods b1 and b2. Sample class includes modules A and B. Sample class has access to all four methods i.e. a1, a2, b1 and b2. So you can see that the class Sample inherits from both modules. Therefore, you can say that the Sample class shows multiple inheritance or mixins .

Ruby - Module Statement - Wuya Tutorial NetworkWuya Tutorial Network provides Module syntax module Identifier statement1 statement2 ........... end module constant name...Wuya Tutorial-Ruby - Module Statementhttps://www.learnfk.com/ruby/ruby-modules.html