What Is The Difference Between Modules And Classes In Ruby

Modules versus classes in ruby

Published:
Last modified:

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

#ClassModule
instantiationcan be instantiatedCan *not* be instantiated
usageobject creationMixin facility. Provides a namespace.
superclassmoduleobject
methodsclass methods and instance methodsmodule methods and instance methods
inheritanceinherits behaviour and can be base for _inheritance_No _inheritance_
inclusioncannot be includedcan be included in classes and modules by using the include command (includes all instance methods as instance methods in a class/module)
extensioncan 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 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·