Modules as Namespaces

A namespace is simply the definition of some set of functionality under one umbrella. You might create a namespace module in situations where you want to provide functionality that can be used without creating objects. For example, the Abbrev module defines only one method, called abbrev, which returns a list of possible abbreviations for a set of words (Figure 9.1). This is such a unique and isolated feature that it really doesn’t merit being part of a class and having instances.

Figure 9.1 An example usage of the abbrev method from the Abbrev module.

image

Of course, you can define methods, variables, and constants outside ...

Get Ruby: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.