What Is The Difference Between Modules And Classes In Ruby

Modules versus classes in ruby

  • Published: August 9, 2016

Overview

A Module in Ruby is a collection of methods and constants.

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

Ruby-doc.org

Classes versus Modules

# Class Module
instantiation can be instantiated Can *not* be instantiated
usage object creation Mixin facility. Provides a namespace.
superclass module object
methods class methods and instance methods module methods and instance methods
inheritance inherits behaviour and can be base for _inheritance_ No _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 extend command (only with inheritance) module can extend instance by using extend command (extends given instance with singleton methods from module)

Table source: Difference between a class and a module

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.

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 ·