It now makes sense to introduce another language feature in ruby.
Module function ruby example.
If used with no arguments subsequently defined methods become module.
We can explain the below example in the.
To embed a module in a class you use the include statement in the class syntax include modulename if a module is defined in a separate file then it is required to include that file using require statement before embedding module in a class.
I e it is not possible to create objects from a module.
The instance method versions are made private.
The require method takes the name of the file to require as a string as a single argument this can either be a path to the file such as lib some library rb or a shortened name such as some library if the argument is a path and complete filename the require method will look there for the file.
The modulo function in ruby returns the modulo value of a number by another number.
And modules unlike classes therefore do not have a.
Module functions are copies of the original and so may be changed independently.
However modules can not be instantiated.
The function takes number1 and number2 whose modulo is returned.
These functions may be called with the module as a receiver and also become available as instance methods to classes that mix in the module.
The ruby include statement simply makes a reference to a named module.
In ruby modules are somewhat similar to classes.
In ruby a module can be used as logical entity.
Examples to implement of ruby modules.
If that module is in a separate file you must use require to drag that file in before using include.
One feature of ruby that makes it such a good fit for frameworks like rails is that you can execute arbitrary code inside class and module.
This is an example where we are creating a module and inside this module we are defining a few methods and a constant and then we are directly accessing these methods and constants with the name of the modules.
Using the require method.
The function returns the modulo value of a number by another number.
Consider the following module.
So this behavior isn t that different from the instance method behavior we saw in the first example.
Instead it makes a reference from the class to the included module.
Inside of a class or module definition.
They are things that hold methods just like classes do.
You can embed a module in a class.
Remember classes and modules are treated as objects in ruby.