3. Modules? Do Tell…
Used to hold a collection of methods and/ or constants
Defined similar to a class
4. How Do We Access This Module?
Need to ‘include’ the module in the class
5. Differences Between A Module And A Class
Provides both state (instance variables) and behavior (instance methods)
Can inherit features from a parent class
Cannot directly inherit features from multiple parent classes (multiple
inheritance)
Only provides behavior (instance methods as well as class methods)
Cannot inherit from a class
Cannot make an instance of a module
Class
Module
6. A Class Can Inherit Methods From Multiple Modules
7. Mix It Up: Black Thursday
Project has multiple classes with some repetitive methods
Create a module that holds these repetitive methods
‘Include’ this module in the classes that have the repetitive code
Eliminate around 24 methods across these classes
Will need to add one or two methods per class to get this to work
Code will look tighter and cleaner
Editor's Notes
Modules are essentially used to hold a collection of methods
Defined similar to a class
Can define a module as shown
We can now make that method available to the class as if it were defined in the class itself
To access the methods of a module in a class, we need to ”include” the module in the class
By including (or mixin’ in) our module ‘AwesomeName’, we have made the method ‘some_method, available to our class
The method is available even though we don’t specifically see it listed anywhere in the class
We could thus call ‘some-method’ as shown
A module is similar to a class, but whereas a class provides both state and behavior, a module only provides behavior
A module can’t inherit from a class and one can’t make an instance of a module
On the flip side, a class can inherit features from a parent class but cannot inherit features from multiple parent classes
Ruby modules pave the way to sidestep this drawback, by allowing a class to inherit functionality from multiple modules, by way of mixins
Let’s say we have 3 different modules, each having a method
If we now include these 3 modules in our ‘NotSoAwesome’ class, we can allow that class to inherit all the 3 methods (and the corresponding functionality) from these modules
Modules are also great as they allow us to share common methods between multiple classes, by ‘mixin’ them into the classes
We could thus give any number of classes, access to the methods in these modules
Here class A and class B have access to all the 3 methods, across 3 modules
I believe you all are going to be working on your final paired project which to me was super interesting. Is it Black Thursday?
Perfect project to try using modules
Essentially, you will have to work with multiple classes and some of them have repetitive methods
If I remember correctly, there are 6 classes that have 4 methods each that are similar
So create a module that holds this repetitive code and then call that module in those 6 classes
By doing so, you will eliminate around 24 methods across your