Modules and mixins

Define a module

The keyword module groups methods and constants under one name. Unlike a class, a module cannot be instantiated on its own. Classes later borrow the grouped behavior through include or extend.

module Loud def shout(text) puts text.upcase end end puts Loud.class

ruby loud_module.rb

Module

Include a mixin

Writing include inside a class mixes the module methods in as instance methods. Every instance then answers to the shared behavior. The class body stays short while the module carries the reusable part.

module Loud def shout(text) puts text.upcase end end class User include Loud end User.new.shout("hello")

ruby include_demo.rb

HELLO

Extend with class methods

Writing extend inside a class mixes the module methods in as class methods instead. This placement suits finders, builders, and other helpers that belong to the class itself. Instances remain untouched by the mixed behavior.

module Finder def find(id) puts "found #{id}" end end class User extend Finder end User.find(7)

ruby extend_demo.rb

found 7

Modules as namespaces

Modules also fence off names: nesting classes and constants inside keeps each name scoped. A Cart defined inside Shop is addressed as Shop::Cart and never clashes with a Cart from another library.

module Shop class Cart def size 0 end end end puts Shop::Cart.new.size

ruby namespace_demo.rb

0

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does include inside a class do with a module?

    Answer

    Adds the module methods to the class as instance methods

    Every instance of the class then answers to the shared behaviour.

  2. What does this program print? module Greet def hi "hi" end end class User include Greet end p User.new.hi

    Answer

    "hi"

    include makes hi an instance method, so the new User answers it.

  3. What does this program print? module Finder def find 7 end end class User extend Finder end p User.find

    Answer

    7

    extend puts the method on the class object itself, so User.find answers 7.

  4. What does this program print? module Loud def shout(text) puts text.upcase end end puts Loud.class

    Answer

    Module

    A module is an object of class Module, while a class is an object of class Class.

  5. What does Loud.new raise when Loud is a module?

    Answer

    NoMethodError, because a module has no new

    A module cannot be instantiated on its own; a class that includes it is the way in.

  6. A class definition is class Cart inside module Shop. How is the class addressed from outside?

    Answer

    Shop::Cart

    The :: operator reaches a constant or class nested inside the module.

  7. What does this program print? module Shop class Cart def size 0 end end end puts Shop::Cart.new.size

    Answer

    0

    The nested class is created as usual and Shop::Cart is its full name.

  8. Which pair describes include and extend correctly?

    Answer

    include adds instance methods, extend adds class methods

    include mixes into instances, extend into the object it is called in, here the class.

  9. A class uses extend Finder. What happens when an instance calls find?

    Answer

    It raises NoMethodError, because find lives on the class, not on instances

    Instances remain untouched by extend, which is the usual mistake with the two keywords.

  10. Why put class Cart inside module Shop?

    Answer

    It scopes the name, so it never clashes with a Cart from another library

    A module used as a namespace fences off names; it is not a privacy or visibility tool.

Article author: Arthur Isaev