Modules

christian.dinh2481 total contributions
Published Jul 29, 2021Updated Sep 9, 2021
Contribute to Docs
In Ruby, modules are collections of classes, methods, and constants. They lend extra functionality to classes that include them.
Syntax
Modules are defined with a module...end
block. For example:
module ModuleNameinclude OtherModuleCONSTANT = 123def method_1...enddef method_2...endend
Usage in a Class
module TwoTermSolverdef add(a, b)a + benddef subtract(a, b)a - benddef multiply (a, b)a * benddef divide(a, b)begina / brescue ZeroDivisionErrorputs "Division by zero (0) is not allowed."endendendclass Calculatorinclude TwoTermSolverendputs Calculator.new.add(3, 4) # Output: 7
All contributors
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions
- BrandonDusch580 total contributions
- christian.dinh
- Anonymous contributor
- BrandonDusch
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.