Defining Modules
Namespaces and methods.
Defining Modules is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Namespaces and Methods
A module is a container for methods and constants. Unlike a class, you cannot create an instance of a module. Modules serve two roles: grouping code (namespacing) and sharing behavior (mixins).
module Greeter
def self.hello
"Hello from the module"
end
end
puts Greeter.helloModule Methods
Define a method with self. to call it directly on the module, like a utility function. These are often called module functions.
module MathTools
def self.square(x)
x * x
end
end
puts MathTools.square(7)Module Constants
Modules hold constants too. Access them with the scope operator ::.
module Physics
GRAVITY = 9.81
end
puts Physics::GRAVITYNamespacing Classes
Wrap classes in a module to avoid name collisions. The full name uses ::.
module Store
class Item
def initialize(name)
@name = name
end
def to_s
@name
end
end
end
puts Store::Item.new("Book")Why Namespaces Matter
Two libraries might both define a Logger. Namespacing keeps them apart: Audio::Logger and Network::Logger never clash.
module Audio
class Logger
def name; "audio logger"; end
end
end
module Network
class Logger
def name; "network logger"; end
end
end
puts Audio::Logger.new.name
puts Network::Logger.new.namemodule_function
module_function makes the following methods callable both on the module and as private instance methods when mixed in. It avoids repeating self..
module Calc
module_function
def add(a, b)
a + b
end
end
puts Calc.add(2, 3)Grouping Related Constants
Modules are great for organizing related configuration values under one clear name.
module Color
RED = "#FF0000"
GREEN = "#00FF00"
BLUE = "#0000FF"
end
puts Color::GREENNested Modules
Modules can nest inside other modules for deeper organization.
module App
module Config
VERSION = "1.0"
end
end
puts App::Config::VERSIONListing Module Contents
Reflection methods let you inspect a module's constants and methods at runtime.
module Settings
WIDTH = 80
HEIGHT = 24
end
p Settings.constantsModules Cannot Be Instantiated
Trying Module.new on a named module style fails because a module has no constructor for instances. It is a namespace and behavior holder, not a blueprint for objects.
module Tools
end
begin
Tools.new
rescue NoMethodError => e
puts "Cannot instantiate: #{e.message}"
endPutting It Together
Modules organize and share code:
- Use
self.methodfor callable module functions. - Hold constants accessed with
::. - Namespace classes to prevent collisions.
- You cannot instantiate a module.
module Geometry
PI = 3.14159
def self.circle_area(r)
PI * r * r
end
end
puts Geometry.circle_area(2)Quick Check
Test your understanding of modules.
Recap
You learned to define modules:
- Modules group methods and constants.
self.methoddefines callable module functions.::accesses constants and nested classes.- Namespacing prevents naming clashes.
module Util
def self.shout(s)
s.upcase + "!"
end
end
puts Util.shout("ruby")Frequently asked questions
Is the “Defining Modules” lesson free?
Yes — the full text of “Defining Modules” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defining Modules”?
Namespaces and methods. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining Modules” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.