liveBook · Manning

Namespace

As explained in The Well-Grounded Rubyist, Fourth Edition

A Ruby namespace is a naming context that groups definitions and keeps names from different contexts distinct.

Ruby namespaces group definitions under nested names. `Tools::Hammer` and `Piano::Hammer` can refer to different classes without a name collision. Nesting also avoids concatenated names such as `ToolsHammer`, which can become visually cluttered. section 4.4.2

module Tools
  class Hammer
  end
end

Define a class inside a module, then refer to it through the `::` constant lookup token: `Tools::Hammer`. section 4.4.2

A name chain does not reveal whether its final name is a class, module, or plain constant; source or documentation provides that context. The containing name must be a class or module. section 4.4.2

Rake task namespaces

Rake groups tasks under namespaces: `admin:clean_tmp` identifies a task in the `admin` namespace. Nested namespaces can extend the task name, as in `admin:clean:tmp`. section 1.4.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains nested definitions and `::` lookup, contrasts flattened names, and notes that notation alone cannot identify a class, module, or constant.
Examples
Uses `Tools::Hammer` and `Piano::Hammer`, plus Rake tasks named `admin:clean_tmp` and `admin:clean:tmp`.
What you can do after
Build nested class or module definitions and refer to a nested class using `::`.
How it connects
Connects namespaces to Ruby modules, classes, constants, and Rake task naming.

Also explained in

Compare how 20 books explain namespace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.