Skip to content
Poignant Guide Ruby Q&A

What is the difference between a class and a module in Ruby?

Staff answerWritten by Odile Marchetti · Classes, modules and objects0 replies

Short answer

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.

Written by Odile Marchetti · Staff: Ruby basics, objects and the Poignant Guide ·
CitePoignant 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.
  1. Staff: 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 < Animal means Duck inherits from Animal, so it gets name for free. include Swimmable mixes the module's methods in, so Duck objects can swim. Greeter is never included anywhere: it is used as a namespace that holds a constant and a method you call directly on the module. If attr_reader is 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
    # => Module
    

    The practical differences

    Question Class Module
    Can I call new on it? yes no
    Can it inherit from another? one parent, with < no inheritance
    Can it be included in another? no yes, with include
    Can 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 define each.

    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.name methods. 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 Swimmable
    

    Inheriting from a module. class Bad < Swimmable fails with superclass must be an instance of Class (given an instance of Module). Use include instead.

    Including a class. include Duck raises wrong argument type Class (expected Module). Only modules can be mixed in.

    Defining def self.x and then wondering why instances cannot call it. Methods defined with self. belong to the module or class itself, not to the objects. Methods written without self. 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 ancestors list is the lookup order, and super calls the next one down. When a method does something unexpected, print ancestors first. The syntax documentation spells out the lookup order under method lookup.

    Check this in order

    1. Do you need many objects with their own data? Class.
    2. Do you need to share methods between classes that are not parent and child? Module with include.
    3. Do you need to avoid two names colliding? Put them inside a module.
    4. Is an error mentioning Module or Class in a type mismatch? You probably used < where you meant include, or the reverse.
    5. 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.

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

Poignant Guide Ruby Q&A. Questions and answers on learning Ruby and programming: syntax, blocks, objects, gems, Rails, debugging and reading the Poignant Guide.