Modules

Modules are great for grouping methods and classes that are related under a namespace. You can extend modules with a class, and the methods of module will be available on the class that extends it.

module Stats
  def data
    {
      "strength"     => 10,
      "dexterity"    => 5,
      "intelligence" => 3,
    }
  end

  def stats
    data[self.stat_name]
  end
end

class CharacterStats
  # Include Stats as a mixin:
  include Stats
  getter stat_name : String

  def initialize(@stat_name)
  end
end

npc = CharacterStats.new "dexterity"
p npc.stats # => 5