Defining Modules
Namespaces and methods.
Defining Modules is a free Ruby Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
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")Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Defining Modules” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Defining Modules”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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.