465

I came from Java, and now I am working more with Ruby.

One language feature I am not familiar with is the module. I am wondering what exactly is a module and when do you use one, and why use a module over a class?

1

9 Answers 9

546
╔═══════════════╦═══════════════════════════╦═════════════════════════════════╗
║               ║ class                     ║ module                          ║
╠═══════════════╬═══════════════════════════╬═════════════════════════════════╣
║ instantiation ║ can be instantiated       ║ can *not* be instantiated       ║
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
║ usage         ║ object creation           ║ mixin facility. provide         ║
║               ║                           ║   a namespace.                  ║
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
║ superclass    ║ module                    ║ object                          ║
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
║ methods       ║ class methods and         ║ module methods and              ║
║               ║   instance methods        ║   instance methods              ║
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
║ inheritance   ║ inherits behaviour and can║ No inheritance                  ║
║               ║   be base for inheritance ║                                 ║
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
║ inclusion     ║ cannot be included        ║ can be included in classes and  ║
║               ║                           ║   modules by using the include  ║
║               ║                           ║   command (includes all         ║
║               ║                           ║   instance methods as instance  ║
║               ║                           ║   methods in a class/module)    ║
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
║ extension     ║ can not extend with       ║ module can extend instance by   ║
║               ║   extend command          ║   using extend command (extends ║
║               ║   (only with inheritance) ║   given instance with singleton ║
║               ║                           ║   methods from module)          ║
╚═══════════════╩═══════════════════════════╩═════════════════════════════════╝
6
  • 13
    I got the hierarchy, Class -> Module -> Object -> BasicObject. Cool!!
    – Aashish P
    Oct 15, 2012 at 12:22
  • Why does "module consists of" omit variables, when classes and modules both support class variables? See accepted answer to stackoverflow.com/questions/5690458/…
    – kaleidic
    Aug 6, 2014 at 15:40
  • A lot of diagrams in all these answers. A little running example: rubyfiddle.com/riddles/06081
    – Donato
    May 28, 2015 at 22:18
  • 23
    How can a module "not be instantiated" and yet have instance methods?
    – devius
    Oct 7, 2015 at 10:28
  • 2
    @devius as iamnotmaynard already commented on another answer: Modules can add instance methods to the including class.
    – Daniel
    Feb 3, 2017 at 13:27
429

The first answer is good and gives some structural answers, but another approach is to think about what you're doing. Modules are about providing methods that you can use across multiple classes - think about them as "libraries" (as you would see in a Rails app). Classes are about objects; modules are about functions.

For example, authentication and authorization systems are good examples of modules. Authentication systems work across multiple app-level classes (users are authenticated, sessions manage authentication, lots of other classes will act differently based on the auth state), so authentication systems act as shared APIs.

You might also use a module when you have shared methods across multiple apps (again, the library model is good here).

8
  • 7
    Is the module same as Interfaces in java? Jun 6, 2012 at 5:53
  • 16
    @Caffeine not really because Ruby modules actually include implementations, whereas interfaces in Java are abstract Jan 12, 2013 at 6:34
  • 8
    No, Modules and Java Packages/JARs are completely different beasts. Jun 10, 2013 at 15:48
  • 9
    I's more like abstract classes with method implementation.
    – Automatico
    Nov 18, 2013 at 14:47
  • 2
    Actually, @Chole hits upon one of the nice things about modules: Namespacing. So while Modules are not a direct equivalent to packages in Java, it can be used to achieve something similar: blog.rubybestpractices.com/posts/gregory/…
    – michaelok
    Mar 15, 2015 at 18:47
100

I'm surprised anyone hasn't said this yet.

Since the asker came from a Java background (and so did I), here's an analogy that helps.

Classes are simply like Java classes.

Modules are like Java static classes. Think about Math class in Java. You don't instantiate it, and you reuse the methods in the static class (eg. Math.random()).

5
  • 14
    But modules can also add instance methods to the including class, while static classes in Java cannot. Oct 9, 2013 at 16:09
  • 5
    This statement is also true coming from a heavy C# background. Oct 10, 2014 at 23:48
  • 5
    This isn't entirely true; modules don't have static methods, they just have methods. Modules can "extend themselves" (the syntax is actually extend self), making their methods available to their self's metaclass. This makes it possible to dispatch a method like random() on a Math module. But by their nature, a module's methods cannot be called on the module's own self. This has to do with Ruby's notion of self, its metaclasses, and how method lookup works. Check out "Metaprogramming Ruby" - Paolo Perlotta for details. Apr 30, 2015 at 21:51
  • I'd say modules are more similar to interfaces with methods in them(Java 8 interfaces with default impl) but can't inherit one from another unlike java interfaces Jun 16, 2017 at 15:41
  • How this answer has so many votes? btw that was said in better words 1mo before: stackoverflow.com/a/17027346/986862 Jan 15, 2019 at 18:04
42

Basically, the module cannot be instantiated. When a class includes a module, a proxy superclass is generated that provides access to all the module methods as well as the class methods.

A module can be included by multiple classes. Modules cannot be inherited, but this "mixin" model provides a useful type of "multiple inheritrance". OO purists will disagree with that statement, but don't let purity get in the way of getting the job done.


(This answer originally linked to http://www.rubycentral.com/pickaxe/classes.html, but that link and its domain are no longer active.)

1
  • Yep, this is how it works. As such, modules are not comparable to Java's "static" classes; the proxy superclass (some call it a "metaclass") becomes the receiver of the module's method dispatch messages, which makes it more comparable to a static class in Java, and its methods work like static methods. The same is true, however, for Ruby's classes, which can take on "static"-like methods by extending a class. Ruby doesn't actually distinguish between "instance" and "class/static" methods at all, only the receivers of them. Apr 30, 2015 at 22:00
12

namespace: modules are namespaces...which don't exist in java ;)

I also switched from Java and python to Ruby, I remember had exactly this same question...

So the simplest answer is that module is a namespace, which doesn't exist in Java. In java the closest mindset to namespace is a package.

So a module in ruby is like what in java:
class? No
interface? No
abstract class? No
package? Yes (maybe)

static methods inside classes in java: same as methods inside modules in ruby

In java the minimum unit is a class, you can't have a function outside of a class. However in ruby this is possible (like python).

So what goes into a module?
classes, methods, constants. Module protects them under that namespace.

No instance: modules can't be used to create instances

Mixed ins: sometimes inheritance models are not good for classes, but in terms of functionality want to group a set of classes/ methods/ constants together

Rules about modules in ruby:
- Module names are UpperCamelCase
- constants within modules are ALL CAPS (this rule is the same for all ruby constants, not specific to modules)
- access methods: use . operator
- access constants: use :: symbol

simple example of a module:

module MySampleModule
  CONST1 = "some constant"

  def self.method_one(arg1)
    arg1 + 2
  end
end

how to use methods inside a module:

puts MySampleModule.method_one(1) # prints: 3

how to use constants of a module:

puts MySampleModule::CONST1 # prints: some constant

Some other conventions about modules:
Use one module in a file (like ruby classes, one class per ruby file)

1
  • 1
    “- access methods: use . operator - access constants: use :: symbol” only this answer mentioned this!
    – Qiulang
    Jul 3, 2018 at 4:07
8

Module in Ruby, to a degree, corresponds to Java abstract class -- has instance methods, classes can inherit from it (via include, Ruby guys call it a "mixin"), but has no instances. There are other minor differences, but this much information is enough to get you started.

5

Bottom line: A module is a cross between a static/utility class and a mixin.

Mixins are reusable pieces of "partial" implementation, that can be combined (or composed) in a mix & match fashion, to help write new classes. These classes can additionally have their own state and/or code, of course.

3

Class

When you define a class, you define a blueprint for a data type. class hold data, have method that interact with that data and are used to instantiate objects.

Module

  • Modules are a way of grouping together methods, classes, and constants.

  • Modules give you two major benefits:

    => Modules provide a namespace and prevent name clashes. Namespace help avoid conflicts with functions and classes with the same name that have been written by someone else.

    => Modules implement the mixin facility.

(including Module in Klazz gives instances of Klazz access to Module methods. )

(extend Klazz with Mod giving the class Klazz access to Mods methods.)

3

First, some similarities that have not been mentioned yet. Ruby supports open classes, but modules are open too. After all, Class inherits from Module in the Class inheritance chain and so Class and Module do have some similar behavior.

But you need to ask yourself what is the purpose of having both a Class and a Module in a programming language? A class is intended to be a blueprint for creating instances, and each instance is a realized variation of the blueprint. An instance is just a realized variation of a blueprint (the Class). Naturally then, Classes function as object creation. Furthermore, since we sometimes want one blueprint to derive from another blueprint, Classes are designed to support inheritance.

Modules cannot be instantiated, do not create objects, and do not support inheritance. So remember one module does NOT inherit from another!

So then what is the point of having Modules in a language? One obvious usage of Modules is to create a namespace, and you will notice this with other languages too. Again, what's cool about Ruby is that Modules can be reopened (just as Classes). And this is a big usage when you want to reuse a namespace in different Ruby files:

module Apple
  def a
    puts 'a'
  end
end

module Apple 
  def b
    puts 'b'
  end
end
 
class Fruit
  include Apple
end
 
 > f = Fruit.new
 => #<Fruit:0x007fe90c527c98> 
 > f.a
 => a
 > f.b
 => b

But there is no inheritance between modules:

module Apple
  module Green
    def green
      puts 'green'
    end
  end
end
 
class Fruit
  include Apple
end

> f = Fruit.new
 => #<Fruit:0x007fe90c462420> 
> f.green
NoMethodError: undefined method `green' for #<Fruit:0x007fe90c462420>

The Apple module did not inherit any methods from the Green module and when we included Apple in the Fruit class, the methods of the Apple module are added to the ancestor chain of Apple instances, but not methods of the Green module, even though the Green module was defined in the Apple module.

So how do we gain access to the green method? You have to explicitly include it in your class:

class Fruit
  include Apple::Green
end
 => Fruit 
 > f.green
=> green

But Ruby has another important usage for Modules. This is the Mixin facility, which I describe in another answer on SO. But to summarize, mixins allow you to define methods into the inheritance chain of objects. Through mixins, you can add methods to the inheritance chain of object instances (include) or the singleton_class of self (extend).

Not the answer you're looking for? Browse other questions tagged or ask your own question.