Cat, mouse, inception…

Modules in Ruby: Part II

Tech - RubyCademy
RubyCademy
Published in
3 min readDec 5, 2018

--

In this article, we’re going to explore the following topics:

  • Module.new
  • Anonymous modules
  • Module and Class

Feel free to read the Part 1 if you’re not familiar with namespacing and mixin facility in Ruby

Introduction

Ruby is a fully object-oriented programming language.

As we’ve seen in the article about classes in Ruby, a class is in reality an instance of the Class class.

So, as you guess, a module is strongly correlated to Module.

Module.new

In the part I we’ve seen that in Ruby, the module keyword allows you to define a new module

Let’s see how to define the exact same module in Ruby without using the module keyword

The Module.new method returns an object of type Module.

This object is what we commonly call a module in Ruby.

By default, an instance of Module can be assimilated into the concept of an anonymous module.

But, the fact of storing this instance in a constant — the Greeting one for example — changes the state of this instance from anonymous to named module.

This means that this instance will internally keep track of this constant and refer to it every time is needed.

Note that we can access the name of a Module instance via the Module#name method.

Anonymous modules

As we’ve seen in the above section, when a fresh instance of Module is assigned to anything but a constant, then the module remains anonymous

Here the m variable contains an instance of Module.

A call to m.name returns nil as the module is anonymous.

So we refer to that instance as an anonymous module.

What happens if we assign this anonymous module to a constant?

Here we can see that the m module passes from an anonymous to a named module after assigning it to a constant.

We can now use the m.yo method by calling Mod.yo.

Also, note that a call to the m.name method returns "Mod" after the m variable is assigned to the Mod constant.

Module and Class

Module and Class are not only friends syntactically.

They also share two secrets.

Let’s have a look to the superclass of the Class class to figure it out

irb> Class.superclass
=> Module

Class directly inherits from Module!

So Class is a specialization of Module.

Now, let’s see what’s Module?

irb> Module.class
=> Class

Module is an instance of the Class class!

So, to recap:

Definition of a class in Ruby

A class is an instance of the Class class stored in a constant.

The Class class is a specialisation of the Module class whose instances can be instantiated.

Definition of a module in Ruby

A module is an instance of the Module class stored in a constant.

The Module class is an instance of the Class class stored in a constant and whose instances cannot be instantiated.

Ruby Mastery

We’re currently finalizing our first online course: Ruby Mastery.

Join the list for an exclusive release alert! 🔔

🔗 Ruby Mastery by RubyCademy

Also, you can follow us on x.com as we’re very active on this platform. Indeed, we post elaborate code examples every day.

💚

--

--