Crystal by example: Modules
Modules in Crystal are used to organize code and share functionality. They can contain methods, constants, and types, but they cannot be instantiated.
Using modules as namespaces is simple—use the :: operator to access what is inside the module.
include adds the module’s methods as instance methods to the class or struct that includes it, and extend adds the module’s methods as class methods, tied to the class or struct itself.
Inside the module, self can be used to create methods that belong to the module itself.
Modules do not allow inheritance, but they can be included in multiple classes or structs to reuse code.
module Constants PI = 3.145 TAU = PI * 2 E = 2.718end
puts Constants::PI
module Greet def hello puts "Hello!" endend
class Person include Greetend
s = Person.news.hello
module MathHelpers def self.square(x) x * x endend
puts MathHelpers.square(5)
module Trackable def track puts "Tracking #{self}" endend
class Car include Trackableend
class Bike include Trackableend
Car.new.trackBike.new.track$ crystal run modules.cr
3.145Hello!25Tracking #<Car:0x23c4dc96fb0>Tracking #<Bike:0x23c4dc96fa0> Next example: Visibility