Classy Modules
A look at classes and modules in Ruby
April 23, 2015
On the surface classes and modules look very similar in Ruby.
But even though they both encapsulate several methods, Classes are more like factories and Modules are more like libaries.
Factories pump out objects. That's exactly what classes do when you call Class.new
. A new object is created that inherits all the attributes of the class. If it's a kayak factory the kayak object that is created has attributes like length, color, and number of seats.
But you wouldn't get that from a module.
Libraries give you knowledge. You go into them and reference all the different pieces of wisdom that humankind have created. You pull out what you need.
In a library you could learn where to paddle your kayak. How to fish off of your kayak. How to put your kayak on your car. These are all bits of knowledge that you can reference.
Modules work the same way. Over time you will start to build up a libary of methods that take care of specific jobs. You don't want to re-write these methods from scratch, so you put them in your modules and bring those modules into your code as needed.
One example would be a "login" module. If every web site you build requires authentication to work, then you'll be writing login code over and over. Unless you do it right once and plop it in your library.
Then you bring in your module and use what methods you need.