home PYTHONJAVA
 

Ruby Module

Modules are a method for consolidating techniques, classes, and constants. Modules offer both of you benefits.

    The
  • module gives a namespace and maintains a strategic distance from name impacts.
  • The
  • module executes the mixin gadget.

Modules characterize a namespace, proportionate to a sandbox, where your techniques and constants don't struggle with strategy constants somewhere else.

The module is like the class, however there is a difference:

  • Module can't be instantiated
  • Module has no subclasses
  • Modules must be characterized by another module

grammar

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

Module steady naming is like class consistent naming, beginning with a capitalized letter. Strategy definitions seem to be comparable: module technique definitions are like class strategy definitions.

Through the class technique, you can call the module strategy by putting the module name and a speck before the class strategy name. You can utilize the module name and two colons to reference a consistent.

Instance

#!/usr/bin/ruby # a 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 name but different functions:

Instance

#!/usr/bin/ruby # Defined in moral.rb Module in the file module Moral VERY_BAD = 0 BAD = 1 def Moral.sin(badness) # ... end end

Just like the class method, when you define a method in a module, you can specify that the module name is followed by a 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 of the defined modules, you can simply load the module file using the Ruby require statement:

grammar

grammar

require filename

The file extension .rb is not required here.

Instance

$LOAD_PATH << '.' require 'trig.rb' require 'moral' y = Trig.sin(Trig::PI/4) wrongdoing = Moral.sin(Moral::VERY_BAD)

Here, we use $LOAD_PATH << '.' to let Ruby realize that the ebb and flow catalog must be hunt down the referenced record. On the off chance that you would prefer not to utilize $LOAD_PATH, you can utilize require_relative to reference a record from a relative registry.

Note: Here, the record contains a similar capacity name. Along these lines, this prompts code uncertainty when alluding to the calling program, however the module dodges this code equivocalness, and we can call the fitting capacity with the name of the module.

Ruby include statement

You can insert modules in your class. To implant a module in a class, you can utilize the include articulation in your class:

Syntax

include modulename

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

Instance

Assume the following modules are 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 reference the module in a class as follows:

Instance

#!/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 results:

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

Mixins in Ruby

Before perusing this segment, you have to get a look at item situated ideas.

When a class can acquire the properties of a class from various parent classes, the class shows up as numerous legacy.

Ruby doesn't legitimately bolster various legacy, however Ruby's module has another mysterious component. It for all intents and purposes wipes out the requirement for different legacy and gives a gadget called mixin.

Ruby doesn't generally execute numerous legacy instruments, yet rather utilizes mixin innovation as a substitution. The module is incorporated into the class definition, and the techniques in the module are blended into the class.

Let's investigate the example code underneath to become familiar with mixin:

Instance

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 comprises of strategies a1 and a2.
  • Module B comprises of strategies b1 and b2.
  • Class Sample contains modules An and B.
  • Class Sample approaches each of the four techniques, a1, a2, b1, and b2.
  • So, you can see that the class Sample acquires two modules, you can say that the class Sample utilizes various legacy or mixin .






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.