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.

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

  • http://www.startahead.be Jan Caals

    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!

  • http://www.rubyfleebie.com Frank

    @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!

  • Oren

    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!

  • http://www.rubyfleebie.com Frank

    Thanks for the kind words Oren. Again, it’s really appreciated

  • Pingback: GSIY … Ruby-Rails Portal()

  • http://apirate.iwasmeantto.be Sam Figueroa

    This really got me up to speed again on modules. I only looked at them half heartedly in the past. This and the follow-up got me going. Thanks for the great article.

  • http://www.donnelford.cn Ivan

    Nice

  • http://allan-ford.strohlchevrolet.cn Iannis

    Nice

  • Pingback: juegos casino ruleta()

  • Pingback: westernunion pocher on line()

  • dev

    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

  • http://www.rubyfleebie.com Frank

    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!

  • http://adhirajrankhambe.wordpress.com Adhi
  • Pingback: What I love most about Ruby « iHack, therefore iBlog()

  • Pingback: Ruby Modules Explained | Jim McKerchar()

  • http://jimbocortes.posterous.com jimbocortes

    Great article explaining ruby modules. Keep it up.

  • Aleksandar

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

  • http://www.rubyfleebie.com Frank

    Thanks Aleksandar and everyone else for your good words!

  • Pingback: Ruby on Rails Resource List | Adriana Galue's Blog()

  • Christian Rojas

    Thanks, good example!

  • http://bestofbritishonline.com/prodimages/AMBR001_L.htm wholesale cheap Knicks jerseys china

    Cavaliers with lowest price and easy returns service.

  • http://nilas.me nilas

    Excellent explaination, thanks :)