January 2019
Intermediate to advanced
246 pages
5h 23m
English
Different libraries sometimes use the same names for methods or variables, leading to unwanted collisions. To distinguish these from each other, you need an additional name—the name of the module the object is defined in.
Modules structure code by defining namespaces. You can use a module to contain a complete implementation of related functionality, including constants, methods, classes, and even other modules. You saw a first example in Organizing Code in Classes and Modules. Here’s another one:
| | module Crystals |
| | class Rhombic |
| | end |
| | |
| | class Triclinic |
| | end |
| | end |
| | |
| | t = Crystals::Rhombic.new |
| | typeof(Crystals) # => Class |
Note the namespace notation :: in
Read now
Unlock full access