An introduction to modules : Part 1

Almost every languages have variables, constants, classes and objects… but who knew about modules before Ruby was invented? A ruby module is not that easy to define because it can serve two different purposes. If we introduce the word Mixin in our definition, we’re diving into the more complex side of what modules are. Today, I will talk about modules without talking about the mixin functionality.
A module without the mixin functionality?
If the concept of mixins didn’t exist, modules would be so simple… and perhaps a little bit boring. At its heart, a module is a component used to logically regroup similar things. By doing so, 3 good things happen : #1 Your code becomes more organized. #2 You eliminate the risk of name clashes. #3 You become rich, extremely smart AND you become able to move very heavy stuff with the power of your mind. Today, I’d like that you concentrate on the 2 first points. You probably already guessed it right : This is the definition of a namespace.
Sometime, when the application you’re working on is rather big, you may need an additional level to regroup things together. May it be methods, constants or classes, it can makes sense to put all of these things under a common parent for the sake of understanding.
But aren’t classes fit this purpose?
Yes… but not always. Generally, a class contains variables and methods that describe the state and the behavior of an entity (a dog, a user, a story, etc.). A module, on the other hand, is just a name that regroup whatever you think is related. For example, say I have the class “Dog”. This class contains several variables and methods that describe the state and the behavior of a dog. Now, say that I also have a constant called NBR_OF_DOGS_NEEDED_TO_START_A_DOG_PARTY. I *could* put this constant in the class Dog… but it would not be a good idea all that much. This constant isn’t an attribute that describe the state of a dog, it is just something that is related to dogs in general. An idea could be to define a module named DogsRelated and put the class “Dog” as well as the constant NBR_OF_DOGS_NEEDED_TO_START_A_DOG_PARTY in it.
Another benefit of doing this is that this new module will “protect” the names that it contains. So if you have installed a 3rd party library that also contains a class named Dog… you will still be able to specify which of these classes you want to use at any moment.

module DogsRelated
  class Dog
    def bark
      puts "Woof..."
    def eat
      puts "..."
    def wag_tail
      puts "I'm doing this because I'm happy"
    def give_the_paw
      puts "Why??"
x = 6
charlie =
charlie.wag_tail if x >= DogsRelated::NBR_OF_DOGS_NEEDED_TO_START_A_DOG_PARTY

We use the “::” notation to access the constants located inside the module. (The class Dog is also a constant). Remember that the dot “.” is reserved for methods calling.
… and that’s about it. But wait, there is more.
The real interest of ruby modules comes with mixins … more on that in the next part

23 thoughts on “An introduction to modules : Part 1

  1. Just a comment in general. I really like your articles. You manage to explain some of the more obscure facets of Ruby in a very understandable way. Thanks and keep up the good work!

  2. @Jan Caals, Thanks a lot for your comment. This is the kind of comment that gives me confidence and a nice little boost 🙂 Thanks again and I’m glad you like the articles!

  3. great post.
    I agree with Jan – your writing style is what is missing in a lot of book and tutorials.
    I am waiting for the second part!

  4. Hi, did u know how to include this module into other ruby file?
    let say i have a .rb file name module_example and inside this file has a module call Module_Ex
    and at other .rb file name module_example_call, when i include the module_example.rb and include it module by coding
    require ‘module_example’
    include ‘Module_Ex’
    when i ran, it say no such file to load?
    any idea

  5. Hello dev,
    The problem is with your “include” call. You must specify the name of the module without the quotes. Try this : include Module_Ex
    Hope this helps!

  6. Very good explanation. I am C programmer and it is nice to learn new things about Ruby especially when it is explained so nicely.

Leave a Reply

Your email address will not be published. Required fields are marked *