Ruby modules are similar to ruby classes. They're large groups of methods that fall under a category. So an example of a built in ruby module would be the Math module.
We can create our own custom modules using the current ruby module syntax. It looks like this:
module Somename #loads of methods in-between. end
Simple repeatable tasks are great for modules. If there's something you're going to use over and over I recommend creating a module to add it to it.
module Greeting def hi puts "Hi!" end def hello puts "hello" end def gmorning puts "Good Morning" end def hola puts "hola" end end greet = include Greeting greet.gmorning #=> Good Morning greet.hello #=> hello greet.hola #=> hola greet.hi #=> Hi!
In the code, the module,
Greeting, has 4 methods: gmorning, hello, hi, and hola. The keyword,
include, allows the module and it's method to be used. The variable
greet, is an instance of
Greeting, and can access all of it's methods. So, once we do,
greet.hi the output will be:
You sure it works?
Here's the code, run it and see what happens.
Hope you learned somehting!😎