← Home · Ruby skill tree

An interactive Ruby lesson

Modules

A module groups reusable methods. Use include inside a class to make the module's instance methods available on objects of that class. Start with Classes.

p displays an inspected value. Predict the output, press Run, then open the expected output. Change one input and run again. Reset restores that example; Stop ends a running example.

The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so keep all module and class definitions with their calls in the editor you run. The code and answers remain readable without JavaScript.

1. Include a method

module NumberTools begins a module definition. The name starts with an uppercase letter. The inner def and end define double; the outer end closes the module.

include NumberTools inside Calculator makes the module's instance methods available on Calculator objects. Calculator.new creates the object; calculator.double(4) calls the included method on it. Use the including class to create an instance. Predict the output.

Show expected output
8

Try it: Change the argument 4 to 0, then to -3. Predict each result.

A module can group several methods. This NumberTools defines double and add_one. Both return their last calculation. The same calculator object can call either method. Predict both lines.

Show expected output
6
4

Try it: Change the argument in add_one(3) to 0. Predict which line changes. Then try calculator.add_one(calculator.double(3)) as the second call.

3. Reuse a module across classes

Calculator and ScoreHelper each include NumberTools. Objects from both classes can use its double method. This reuse of a module's methods is called a mixin. Each call below runs on a different object. Predict both results.

Show expected output
6
10

Try it: Change number * 2 to number * 3 in the module. Predict how both classes' results change.

4. Update the receiving object

Counter includes CounterUpdates and initializes @value before add uses it. When you call counter.add(3), the included method reads and updates that counter's @value. The value method reads the same object's stored number. Predict the output.

Show expected output
8

Try it: Change counter.add(3) to counter.add(0), then try counter.add(-2). Predict each result. Add a second update before displaying the value.

5. Keep two objects separate

Each Counter.new call creates an object with its own @value. first.add(3) runs the included method on first. Its numeric update leaves second with its starting number. Predict both lines.

Show expected output
5
10

Try it: Change first.add(3) to second.add(3). Predict both lines. Then alternate updates on the two objects and create a third counter with a starting value of 2.

6. Call an included helper

doubled_value is an instance method written in Counter. Its double(@value) call uses the included method on the same counter object.

This calculation has no assignment to @value, so the counter keeps its stored number. Predict the result and the stored value.

Show expected output
8
4

Try it: Change number * 2 to number * 3. Predict both lines. Then try a starting value of 0 or a negative number.

Practice on your own

Replace the code in the last editor with a program for these tasks. Predict the output, then run it.

  1. Define CounterUpdates with add(amount). Add the amount to @value and assign the result back.
  2. Define Counter with include CounterUpdates, initialize(starting), and value. Store the starting number in @value.
  3. Create first = Counter.new(10) and second = Counter.new(1). Call first.add(5) and second.add(2).
  4. Display first.value, then second.value. Explain how the object receiving each call determines which @value changes, even though add is written in the module.
Show one solution and its output
module CounterUpdates
  def add(amount)
    @value = @value + amount
  end
end

class Counter
  include CounterUpdates

  def initialize(starting)
    @value = starting
  end

  def value
    @value
  end
end

first = Counter.new(10)
second = Counter.new(1)
first.add(5)
second.add(2)
p(first.value)
p(second.value)

Output:

15
3

Change first.add(5) to first.add(0), then to first.add(-5). Predict both lines. Reverse the order of the two update calls and explain whether the final numbers change.

Ruby documentation

See the Ruby 4.0 guides for module definitions and include. The syntax guides explain instance variables, the object receiving a method call, and method return values.

Return to the Ruby skill tree to choose another topic.