Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!


  • A module contains a named group of methods, constants, and class variables
  • a Module object is an instance of the Module class
  • It cannot be instantiated or subclasses
  • It is commonly used for namespaces and mix-ins
  • RDoc:
  • A "mix-in" allows methods from the included module to become instance methods in the class
  • A namespace is a set of names like method names and constants that have scope or context
  • A module is one form of namespace
  • It prevents namespace collisions with other common methods and allows you to group related methods together
  • To create a module, you must start with an uppercase letter for the module name
  • Modules can be included in a class or inherited from another module
  • To load your module, use 'require' or 'load' keyword


Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Modules 0:04
    • What is Modules?
    • Modules Examples
  • Modules: Mix-Ins 3:31
    • What is a Mix-in?
  • Modules: Namespace 4:07
    • What is a Namespace?
    • Why Use a Namespace?
  • Example of a Namespace Module 5:59
    • Example of Mixing in The Module Into the Global Scope
  • Modules: Creation 7:04
    • How to Create a New Module?
  • Modules: Usage 8:19
    • How to Use It?
    • class Poker & class Bridge
  • Creating Our Module as a Mix-In 9:41
    • Example of a Module Using Instance Methods
    • Coding Example
  • Creating Our Module as a Namespace 12:11
    • Implement Class Methods for the Module
    • Coding Example
  • Loading Our Module 19:46
    • Loading Our Module Overview
    • Require & Load
    • Coding Example: Loading Module
  • Lesson Summary 23:36

Transcription: Modules

Welcome back to

Today's lesson is on modules.0002

What are modules? They contain a named group of methods, constants, and class variables--it's kind of like a class.0006

A module object is an instance of the Module class; it's similar to a class, but it cannot be instantiated or subclassed.0021

So, here is one difference we can see between those two.0030

It is used for namespaces, and mixins.0035

Let's get a little more into what this Module is; let's look at some of the RDoc for it.0040

It says here, "a Module is a collection of methods and constants. The methods in a module may be instance methods or module methods."0048

"Instance methods appear as methods in a class when the module is included, module methods do not."0059

"Conversely, module methods may be called without creating an encapsulating object, while instance methods may not."0066

They give us an example with Module; it's called; it's including another module here, and we're ending it.0075

Then you can call some of the class methods: class, constants, instance methods...0090

So, if I call constants, it returns Module here; you can look it up; it turns out an Array--constants.0095

You can call nesting; it "returns the list of Modules nested at the point of call."0106

There is new; that has its own instance methods, too.0110

What's interesting is this ancestors; in this module, if I include other modules, I can call ancestors and see what they are.0121

You can look at more of this RDoc.0130

One important thing I want to show you here, also...let's look at the Class RDoc, because they are very similar.0134

Classes in Ruby are first-class objects--each is an instance of Class Class.0142

If I change Class to Module, I've made a module, essentially.0149

They have this chart here; everything starts at the top, at BasicObject; subclass is that Object; and you will notice that the superclass of Module is Object.0156

What is very interesting is that Class is actually a subclass of Module.0171

They do look very similar, so it makes sense.0176

Class actually holds all these exact same properties of this Module, and it defines the new ones.0179

If you have gone through our Class lesson, a lot of the same things you did there, you can do with these modules.0190

Module is the ancestor of Class, so you don't have all the same functionality there.0197

I did mention mixins; I mentioned namespace; let's talk about that.0211

First, what is a mixin? This is a method from an included module.0216

They become instance methods in the class.0223

If I have this class, I can include this module in, and all of these methods that get included--now they're just like instance methods in the class itself.0228

These methods are known as being mixed in to the class; that is why it's called a mixin.0241

Next, let's look at namespace.0248

What is a namespace? Namespace is a set of names, like method names, and constants that have scope or context.0252

We know that it's a bunch of names, essentially; a module is one form of namespace in Ruby.0268

We have this module; a module is a namespace.0277

A Ruby module associates a single name with a set of methods and constant names.0283

Again, it's very similar to classes; we have this Class that does it, but we have this Module that I can include these methods and constant names--include them in the classes--mix them in to these classes.0288

A Ruby class is another form of a namespace.0301

We have a module; it's a mixin; it's a namespace; it allows both of these.0306

Module namespace; why use a namespace?0314

It avoids adding methods to the global scope, and it prevents collisions with other common methods, and it can be utilized to group other methods together.0320

This is perfect; with this namespace, I can group all these methods together.0331

They're not going to be in the global scope; they're not going to conflict with a bunch of methods; I can scope it all in one namespace, and then I for mixing in to other classes; I can use it to scope in that module itself.0339

This avoids adding those methods to the global scope; so this is nice to have.0353

We'll do an example of a namespace module, an example of mixing in the module into the global scope; let's look at this example.0360

We're going to look at the Math module.0368

First, if I call Math and call the class method cos(0), I'm in the Math namespace.0379

But now, I can call include Math; this actually should just be lowercase: include I do include Math and bring that Math into our global scope.0389

Now, I can just call cosine...this should be lowercase, too...cos(0); and now, I can just call this; I don't have to include Math, that namespace, in.0403

I mixed it in the global scope; not only can you mix in things in classes; you can mix it in the scope of your code, too.0414

Next, let's look at modules and how I create this module.0424

I'm getting excited over what this is; how do I create it?0429

The module name must start with an uppercase letter; so, to create it, I have to add module, and then the module name.0432

On the example, module, then I can just do Foobar...or module needs to be uppercase.0445

That first letter is uppercase; that is important.0460

If you replace class with module, you would have created a module, essentially: very simple.0463

It can contain methods, constants, other modules, and even classes.0472

Example module here: I call module--I have that uppercase letter for Cards--I can put my code here; end it.0477

There!--I just created a module; very simple, but you can make it very complex, depending on how many methods you put in, if you include another module...even classes in there--you can add nested classes.0486

Let's look at the usage.0501

How do I use it?0506

Classes can include a module, and modules can inherit from another module.0514

This is interesting; classes can include a module--we already talked about that; that's the mixin.0523

But also, modules can inherit from another module; so I actually can use this module, include another module, and instead of duplicating that code, I can reuse it in two modules; that's interesting.0533

For this example, we could use it as a mixin; I have a class called Poker, a class called Bridge...I just include Cards; they have the same functionality here.0553

Both of their usage...I'm using it the same way, and I don't even have to repeat myself; I just do include Cards, and all that code that was required for it is in this module, so I just do that.0569

Let's create our module now; now that we're excited, we need to actually create some code here.0583

Example of a module using instance methods: we're including it, and it's going to provide all of these instance methods to our class.0590

I have this module that has Cards; three methods: 1--it's going to retrieve the top card in that card deck, and it's going to return that card.0599

The second one is going to return the card back to the deck; so we have one argument that is going to take a card...0609

And the last one...we're going to just shuffle all of those cards together, so there is going to be code for that.0615

Let's go ahead and create some...look at the code for that.0620

Let's make it more I call module Cards, do retrieve.card, and then I'm just going to do some output here--retrieve.card, and then I have another method called return_card_to_deck, one card...0629

I'm just doing output; I'm not actually implementing deep methods...just to get the concept across.0649

Card...whatever argument is returned to deck...last one is shuffle, and I'm just going to say's shuffling the cards.0655

We have our module; now let's go ahead and include it in one of our classes.0669

We have this class Bridge; we're going to include Cards in there, and--OK--we created a class; we created a module; now, let's see it in action.0674

I call; it's going to create the new Bridge object; and then, I'm just going to say, "OK, I've included this mixin; let's see if I can use it."0684

I can do retrieve_top_card--notice that the instance method is part of it--output is retrieve_top_card.0695

I can also do return_card_to_deck, and I'm going to say it's an ace of spades, so it says ace of spades is returned to deck.0703

Then, I can just B.shuffle, shuffle_cards; all those module methods are part of that instance method in that class.0715

Now, let's look at creating our module as a namespace.0737

Here, we're going to implement those class methods for the module.0743

You can use nested classes to it, also.0752

For this example, we're going to get deeper and actually define some code.0756

I'm going to create this module; it's called Randomizer; it's going to have two class methods.0762

For this example, we're not mixing it in; we're not doing mixins for this namespace; it's essentially just a namespace; we're just scoping into this namespace these class methods.0771

We have this self.generate, and this is going to take one argument; whatever length you give it, it's going to generate some random string of that length.0781

That is why I have this 0..length; it takes the letters a through z, makes it an Array, and randomly chooses one of those letters for each one; and then it joins it all as a String.0796

I'll show it to you in action, too, so you understand.0814

Next, we have this self.scramble token; for this one, I pass in a phrase or word, and it's going to scramble those letters together, and it's going to put them in some random order.0818

It takes this argument 'token'; it's going to break it up; and then it's going to sort them randomly, and then it will just join them together to be random.0829

Before I do that, I want to show you how to nest classes.0843

All I would do is...let's have this module Foobar...I could just do class Bar, and put whatever code I have here...with squigglies...and I can make another class...anyway, you're going to end it here.0847

I can have nested classes like that; it's a good way to organize it, so if I want this namespace, but it's really complex, all this code I'm putting into this module, it's nice to have nested classes to help organize it.0874

That is why you have that there.0890

Let's go ahead and do an example of that.0893

First, let's look at that module Randomizer.0899

We're going to define the code.0905

I have this length; it takes an argument; I'm going to default it to 10, so if you don't pass an argument, it's going to generate a random string of 10.0907

I pass length, I call map, so it's a to z; it's going to convert that to Array, and then I'm going to do join.0916

Let's make sure we have everything here; we have: it's going to make a length--0 to whatever length they give it--it's going to take an Array; randomly choose one of the letters in that Array; and then the last part is going to join it all together.0937

Next, we're going to look at this scramble.0956

It takes a token, whatever string you want to put; it's going to split that up; it's going to sort by ran, and it's going to join that all together.0959

What I want to do here is just show you what this generate does first.0977

I'm going to go here and change length to 10, so it's going to be 0,1,2,3,4,5,6,7,8,9,10; and then, first, I call this map.0983

It does this to_a; I'm not sure what it does here; it's putting a weird statement out.1002

Anyway, let's go back to that; I do rand, and I include join.1017

Let me change that length and define it.1027

Notice how I do 10; it makes them all an Array; and when I call join, it's going to put them together.1032

Also, I want to show you: when I call that a to z, I do to_ary; it's going to get all of those letters and then, using this rand(26), it's going to randomly choose a number from 0 to 25; so it's going to choose a random letter for each one.1042

The next one I want to show you is that scramble.1065

Let's say I have a token=abc; then I do token.split; it splits it and stores them in an Array.1071

Then I call sort_by!{rand}; it's going to randomize it, put different areas--and then, I just call join, and it's going to bring them together. 1084

Now that we have our Randomizer module, let's see it in action--this module as a namespace.1098

I call Randomizer; I call generate; and, when I do generate, notice it's going to get me a random string--whatever--I can put 15 in there; get a bigger one--25; 5; whatever length I want, it's going to give that to me.1105

Now, let's look at the other Randomizer; let's look at scramble.1123

I run abcdef; scramble those letters up; I run it again--notice they are different every time.1130

That is our implementation of the Randomizer module.1143

You see that we have two methods that have different things, but they are all under the scope Randomizer, and it does random stuff, so that is why we call it that.1147

We have our module there; you see our different namespace; we didn't have to include anything in the global scope--we didn't even have to make a function called scramble, one called generate...we put it all in one namespace to keep the scoping clean and easy for reference.1160

Now, let's look at loading our module.1190

Now that we understand modules--we've created them--we have a bunch of modules; we organized them to different files.1196

They're all separate files; their code is really clean; how do I use them?--how do we load this module?1205

The module is complete; you've organized them into multiple files; how do you use them?1212

There are two ways to load them: you can load them, first, using require; the second one is to do load.1215

We have to remember, for Ruby's load path, the file needs to be in Ruby's load path--so I'll show you that.1226

Both of these require different arguments: when I use require, it expects the library name, and if I use load, it expects a complete filename, including extension.1234

Let's see it in action here.1249

I have some code; I have this module, Randomizer; we've already talked about what it does--it has a generate and a scramble.1255

Now, I've included that as a file called Randomizer.rb, and I want to include that into my code.1270

It's in this directory here--you see it there--Randomizer.1277

I'm going to run IRB; first, let me show you load.1283

We're going to show load first; I'm just going to say load(Randomizer.rb); it says 'true'--it was a success.1288

Now I can use it; it's in my namespace, so I just run Randomizer.generate(10), and there you go!1297

Twenty, scramble, abc...Now, let's show you require; I'm going to exit out, so I start with a clean slate--nothing is in memory.1306

IRB; now when I use require for this code, I make sure I'm using it in the same directory, so I'm going to use relative directory path; so I need to load that into my Ruby path.1321

I do load(path), and then I say, "Load the current directory--the one I'm currently at in there."1334

It's going to actually show me in this Array...this last one is the one I'm using--that relative path there.1342

Now that I did that, I just say, "OK, it's in; I just want to require Randomizer--just the library name"; it says true.1351

I do Randomizer.generate(10)...(15), and notice, when I call generate, it makes a bigger string; and it's in my memory; it's working.1359

I can do a Randomizer.shuffle, and there it...oh, no!--what happened here?--undefined method 'shuffle'.1373

Interesting...did I call it something else?--let's see.1386

Oh, scramble! OK.1393

OK, there we go--scramble; and notice, the first time I called scramble, it actually returned the same, but other times it did scramble the letters up.1405

There you have it.1416

Now you know how to create a module, why it's used, what it's used for, how to implement it, and, when you're all done and complete, how to load that module.1422

Other than that, I suggest you go ahead and look at that RDoc and create your own modules, and see how those work, and use them for your own benefit.1436

It will definitely clean up your code, using them as mixins and as namespaces.1448

Other than that, that is the end of our lesson today at; thank you, and see you next time!1452