< Back to Ruby notes

Modules

Breaking groups of methods apart into smaller groups

Resources:


Modules’ main use is having a place separate from classes to store methods or variables. This is useful for:

A basic module looks very similar to a class, like so:

module ExampleModule
  def true(item)
    item ? "It's true!" : "It's false!"
  end
end

The module can then be included in a class, and all its methods and whatever shall be included.

class ExampleClass
  def is_true?(var)
    true(var)
  end
end