What is the difference between a class and a module in Ruby?
A class is a blueprint you can make objects from with new, and it can inherit from one parent class. A module is a bundle of methods and constants that you cannot make objects from. You use it either as a namespace to group names, or as a mixin that you include into classes to share behavior.
Cite
Poignant Guide Ruby Q&A, "What is the difference between a class and a module in Ruby?", https://poignantguide.net/answers/read/what-is-the-difference-between-a-class-and-a-module-in-ruby/, accessed 2026-11-21.- Odile MarchettiStaffStaff: Ruby basics, objects and the Poignant Guide ·
One of each, in irb
Here is a module doing both of its jobs, and a class using it:
module Swimmable def swim "#{name} swims" end end module Greeter PREFIX = "Hello" def self.greet(person) "#{PREFIX}, #{person}" end end class Animal attr_reader :name def initialize(name) @name = name end end class Duck < Animal include Swimmable end d = Duck.new("Dolly") p d.swim # => "Dolly swims" p Greeter.greet("Ann") # => "Hello, Ann" p Duck.ancestors.first(3) # => [Duck, Swimmable, Animal]Duck < Animalmeans Duck inherits from Animal, so it getsnamefor free.include Swimmablemixes the module's methods in, so Duck objects canswim.Greeteris never included anywhere: it is used as a namespace that holds a constant and a method you call directly on the module. Ifattr_readeris unfamiliar, see how attr_accessor, attr_reader and attr_writer work.What the official documentation says
The Ruby syntax documentation on modules and classes opens with the key sentence: "Modules serve two purposes in Ruby, namespacing and mix-in functionality." It then gives Enumerable as a mix-in example, with methods built on
each, and Comparable, built on the<=>method.It also says: "Every class is also a module, but unlike modules a class may not be mixed-in to another module (or class)." The Module class documentation fits with that, since Class is a child of Module. You can check it yourself:
p Duck.is_a?(Module), Swimmable.is_a?(Class) # => true # => false p Class.superclass # => ModuleThe practical differences
Question Class Module Can I call newon it?yes no Can it inherit from another? one parent, with <no inheritance Can it be included in another? no yes, with includeCan it hold a namespace? yes yes, often its main job How many can I use at once? one parent as many as you like Because a class has only one parent but can include many modules, modules are how Ruby shares behavior across unrelated classes. Enumerable is the famous case: arrays, hashes and ranges are unrelated classes that all
include Enumerable, which is why map, each and select work on all of them. Each only has to defineeach.Which one should I write?
Ask what the thing is. If you can say "a Duck" and want many of them with their own data, write a class. If you can say "something that can swim" and want it added to several classes, write a module. If you only need a home for related helper methods with no objects, write a module with
def self.namemethods. Plain "bag of functions" modules are common in Ruby.Mistakes beginners make
Calling new on a module. You get an error because modules do not make objects:
Swimmable.new # NoMethodError: undefined method `new' for module SwimmableInheriting from a module.
class Bad < Swimmablefails withsuperclass must be an instance of Class (given an instance of Module). Useincludeinstead.Including a class.
include Duckraiseswrong argument type Class (expected Module). Only modules can be mixed in.Defining
def self.xand then wondering why instances cannot call it. Methods defined withself.belong to the module or class itself, not to the objects. Methods written withoutself.in an included module are the ones instances get.Expecting an included module to override the class. Method lookup checks the class itself first, then its included modules, with the most recently included module first, then the parent class. Here is the order in action:
module Walk def move "walk" end end module Fly def move "fly, then #{super}" end end class Bird include Walk include Fly end p Bird.new.move # => "fly, then walk" p Bird.ancestors.first(3) # => [Bird, Fly, Walk]The
ancestorslist is the lookup order, andsupercalls the next one down. When a method does something unexpected, printancestorsfirst. The syntax documentation spells out the lookup order under method lookup.Check this in order
- Do you need many objects with their own data? Class.
- Do you need to share methods between classes that are not parent and child? Module with
include. - Do you need to avoid two names colliding? Put them inside a module.
- Is an error mentioning
ModuleorClassin a type mismatch? You probably used<where you meantinclude, or the reverse. - Is the wrong method running? Check
ancestors.
Chapter 5 of Why's (Poignant) Guide to Ruby on this site has sections on inheritance and on modules, and the Greetable example there is the same mixin idea as Swimmable above. The guide's index lists every chapter if you want to read around it. Checked on 12 November 2026.
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.