Ruby Modules: What You Need to Know to Use Ruby on Rails

ruby modulesSince the mid-1990s, the Ruby computer programming language has become ever more popular. Ruby forms the basis of the famous Ruby on Rails web application development technology on which Twitter and several other famous web-based applications run. Of course, Ruby and Ruby on Rails are built upon various individual components, as are all object-oriented languages, such as classes, variables, constants and objects. That means it can be tricky to learn Ruby on Rails, but of course not impossible.

Prior to the rise of Ruby as a programming language, modules – a key facet and among several basic building blocks of the language – simply didn’t exist within the lexicon of programming.  Though descriptions of Ruby and Ruby on Rails, and the interplay of classes and Ruby modules discussed here may seem complex at first glance, they’re actually not. If fact, those new to computer programming often take very quickly to Ruby and training courses such as those offered by – you could even learn the basics of Ruby in just 10 steps.

Purpose of a Module

At base, a Ruby module is a component that logically regroups similar things. Modules are the way in which Ruby methods, classes and constants – these also form the building blocks necessary to Ruby’s operation – can be grouped by similarity to each other. Ruby modules provide two major benefits:

  • They provide what are called “namespaces” and, thus, prevent name clashes.
  • Modules implement or give impetus to Ruby’s “mixin” facility.

Namespaces allow all the different methods and constants you create as you’re writing a computer program in Ruby to reside together without clashing with each other or being eliminated or overwritten by other methods and constants. They bring order to potential chaos, in a manner of speaking, or at least prevent chaos from working its way into your Ruby programming efforts.  This is how a module would look when written in Ruby syntax, or language:

module  Identifier

Writing Ruby modules can be done by beginners new to the programming language after only a short period of genuine study, though there are certainly more advanced coding techniques for setting up modules to enable much greater functionality of the program written using Ruby. offers a variety of Ruby programming courses that will allow you to learn everything from the basics to the advanced aspects of programming with Ruby from scratch.

Relation of Classes and Objects to Ruby Modules

Writing in Ruby, and creating module constants, is simple when you remember a few rules for actually writing it. For one, modules are named just like classes in Ruby: they begin with an initial uppercase letter. In the example above, the module is named “Identifier.” The module is terminated, or given its ending, by the lower case word “end.” In the Ruby hierarchy modules support the higher-level classes.

Ruby is what’s called an “object-oriented” programming language, where the programmer manipulates objects he’s created or written into the language that perform functions in the way data and logic in a more traditional computer language work. At the top of the Ruby hierarchy are classes, or classes of objects, the components in Ruby that really do all the work.

If you think of a car as a class of vehicle and individual car models as objects within the class known as “vehicle” you see why class objects are the workhorses of Ruby, as cars – or classes — of the same brand or model – or objects — are grouped together to perform a set of errands, say, and so that they can be separated from cars – or classes of another maker or brand by other modules – or, again: objects.

In several respects, Ruby modules are very similar to classes that you as the coder using Rudy to write a program are creating. However, unlike classes, you can’t create objects based on Ruby modules and you can’t additionally subclass them as you can with actually Ruby classes. Instead, you take a Ruby module that you’ve written in the manner shown above and you add its function or functionality to the functionality of a Ruby class – in our example, “vehicle” – or specific object (or a specific model of car). Ruby also includes a large library of pre-written modules ready to go for use by programmers everywhere.

Ruby Modules and Functionality Sharing

We’ve already touched on a Ruby module’s function as a namespace that lets you define various methods for the actions that the code you’re writing with Ruby will perform. Using a Ruby module, the names you give to the methods you’ve written won’t be able to clash with the names with other methods you’ve defined elsewhere even though they may even have the same names.

Ruby is fairly unique, though, in that its modules have a second purpose touched on earlier: they allow for functionality sharing between Ruby classes, something only possible because of Ruby modules. If, for instance, a class mixes in a module that particular module’s methods (called “instance methods”) become available to the class as if they had actually been written into the class by the programmer writing the Ruby-based program.

Ruby modules are able to serve two purposes – all without requiring extensive recoding or rewriting or reprogramming – because of “mixins,” which fall in under classes and modules in making the whole Ruby edifice work. also offers a complete A to Z method for learning Ruby and Ruby on Rails, suitable for beginners as well as those with coding experience in other programming languages. 

Ruby Mixins and Ruby Modules

Mixins are basically the written products in Ruby that allow a module to mix in with a class, or several classes, in fact. Mixins themselves, when written and used in the proper manner in Ruby, add a high degree of versatility functionality to any program based on the popular programming language. The math module found in Ruby, for example, can be customized so that it has public functions available to all users allowed to access the particular program written with Ruby as well as private functions available only to users specified by the program’s writer because of the module mixins the writer coded or wrote.

A typical Ruby “module mixin” will be composed of several lines of code that sets up the conditions whereby the module can mix in with the class and improve the functionality not only of the class but of itself. However, you won’t see a line of code to add all this wonderful functionality that’s written “mixin.” Rather, the mixin is a descriptor for the mixing in that occurs between the Ruby modules that exist below Ruby classes inheriting or deriving impetus or direction from those modules. A Ruby module mixin coding sequence might look like this:

module A
def a1
def a2
module B
def b1
def b2
class Sample
include A
include B
def s1

In the string or line of code you can see how two different modules, A and B, are allowed to mix in with the class above it, that class also being programmed to include the two modules, thus increasing the functionality of the class. Here’s how it would break down:

Ruby Module A is made up, or consists of methods a1 as well as a2. Ruby Module B is made up of method b1 and method b2. The class – labeled “Sample” here – includes both Ruby modules A and B. Class “Sample” – where all the objects are being manipulated and told to do things by the Ruby-based program’s user – can now access ALL FOUR METHODS: a1, a2, b1 and b2. “Sample,” as a class is able to inherit behaviors, and increased functionality, from both Ruby modules. All of the above string of code illustrates multiple inheritances, or a MIXIN.

Ruby, and its Ruby on Rail web application development program, is actually quite simple to utilize once you take the time to learn its way of writing code, which is actually also fairly simple. Object-oriented programming languages like Ruby, because it’s objects that are being manipulated and not data relying on logic, also have a high level of security because of the way classes and their objects inherit from modules and modules having mixins.

The illustration of the math module found in Ruby having both a general public functionality and a higher degree or level of functionality available only to certain designated users is an example not only of enhanced functionality but also security. If you want to know more about Ruby and Ruby on Rails and how easy it actually is to write programs and web application using them you should sign up for one, or all, of the Ruby and Ruby on Rails programming courses as soon as possible.