Namespacing in Ruby
Fast recall
Quick revision
Core answer
::, such as Payments::Charge. It prevents unrelated constants with the same short name from colliding, shows which part of the system owns a concept, and gives files and APIs a clearer structure. It should group real responsibilities without creating unnecessarily deep constant paths.Whether you know namespacing uses modules + :: to group classes and avoid name collisions.
Forgetting the :: (writing Charge inside module Payments but referencing it as Charge, not Payments::Charge), or over-nesting.
Namespacing = modules + :: to group related classes (Payments::Charge), avoid collisions, organize code. Rails uses it (Admin::UsersController).
Interview-ready guide
Interview answer
Namespacing groups related Ruby constants under another class or module. If Charge is defined inside module Payments, its full name is Payments::Charge. The :: operator separates the namespace from the nested constant. This prevents a top-level Charge name from having to represent every kind of charge in the application.
A payment service and an order service may both need a class called Charge. Payments::Charge and Orders::Charge can exist together because they are different constants. The prefix prevents a collision and tells the reader which part of the system owns the class.
The code uses the same final class name in two namespaces. Calling Payments::Charge.new selects the first constant, while Orders::Charge.new selects the second. Their instances are unrelated unless the program deliberately gives them a shared parent or module.
Libraries use a top-level namespace to avoid adding many global constants. Rails applications also commonly match a constant such as Admin::UsersController to an admin/users_controller.rb path. Keep namespaces purposeful and fairly shallow: they should show durable ownership, not repeat every folder in the project.
Namespacing in Ruby
Direct Answer
Namespacing in Ruby means placing related constants—usually classes and modules—inside a containing module and referring to them with ::, such as Payments::Charge. It prevents unrelated constants with the same short name from colliding, shows which part of the system owns a concept, and gives files and APIs a clearer structure. It should group real responsibilities without creating unnecessarily deep constant paths.
Deep Dive & Architecture
Ruby stores classes and modules in constants. Defining Charge while the lexical scope is Payments assigns a class object to the Payments::Charge constant. A separate Orders::Charge assignment points to a different class object even though both final names are Charge.
The :: form makes the lookup target explicit. A leading ::, as in ::JSON, starts at the top level, which can resolve ambiguity inside another namespace. Unqualified constants may be found through Ruby's lexical and ancestor lookup rules, so explicit paths are valuable at boundaries where similarly named concepts coexist.
Namespacing also shapes packaging. A gem can put its public constants under one top-level module instead of polluting the application's global constant space. In frameworks with autoloading, the constant path and filesystem path often need to agree, making a coherent namespace part of maintainable project structure.
A namespace is not automatically a good architecture. If every internal class gets several nesting levels, moving code becomes expensive and call sites become noisy. The useful test is whether the prefix communicates durable ownership or prevents a realistic collision.
Code Example
module Payments
class Charge
def label
'payment charge'
end
end
end
module Orders
class Charge
def label
'shipping charge'
end
end
end
Payments::Charge.new.label # => 'payment charge'
Orders::Charge.new.label # => 'shipping charge'
Payments::Charge == Orders::Charge # => false
Common Mistakes & Pitfalls
Forgetting the :: (writing Charge inside module Payments but referencing it as Charge, not Payments::Charge), or over-nesting.
Check your understanding
Answer these without looking back at the article.
- What does a leading `::` mean in a Ruby constant path?
- How can two classes with the short name `Charge` coexist?
- How do namespaces relate to Rails autoloading and file paths?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.