An introduction to modules : Part 1

Post by Frank on August 27, 2007

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
  NBR_OF_DOGS_NEEDED_TO_START_A_DOG_PARTY = 5
  class Dog
    def bark
      puts "Woof..."
    end

    def eat
      puts "..."
    end

    def wag_tail
      puts "I'm doing this because I'm happy"
    end

    def give_the_paw
      puts "Why??"
    end
  end
end

x = 6
charlie = DogsRelated::Dog.new
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

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon

Go back to the original post on Ruby Fleebie

21 Responses to “An introduction to modules : Part 1”

  1. Jan Caals says:

    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. Frank says:

    @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. Oren says:

    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. Frank says:

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

  5. [...] the first part, we learned that a module was a namespace, a way to regroup similar things to help us organize our [...]

  6. Sam Figueroa says:

    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.

  7. regles poker texas holdem…

    Esta noche apuesta dinero portal strip poker on line tavolo multigicotori poker game on line poker casinos internacionales internet…

  8. westernunion pocher on line…

    patches,unsatisfactory untruthfulness postoperative …

  9. dev says:

    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

  10. Frank says:

    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!

  11. [...] An introduction to modules : Part 1 ยป Ruby Fleebie [...]

  12. [...] An Introduction to Modules: Part 1 [...]

  13. jimbocortes says:

    Great article explaining ruby modules. Keep it up.

  14. Aleksandar says:

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

  15. Frank says:

    Thanks Aleksandar and everyone else for your good words!

  16. Christian Rojas says:

    Thanks, good example!

  17. Cavaliers with lowest price and easy returns service.

Leave a Comment