The Ruby Workshop by Akshat Paul
 Peter Philips 
Dániel Szabó
 and Cheyne Wallace

The Ruby Workshop by Akshat Paul
 Peter Philips 
Dániel Szabó
 and Cheyne Wallace

Author:Akshat Paul
, Peter Philips, 
Dániel Szabó
, and Cheyne Wallace
Language: eng
Format: epub, mobi
Publisher: Packt Publishing Pvt. Ltd.
Published: 2019-10-30T00:00:00+00:00


Namespaces

In addition to adding methods to classes and providing out-of-the-box functionality as part of the module, another major purpose of modules is to provide a namespace. A namespace is just what it sounds like: it provides a scope or space for naming. In particular, it provides a space for constants. With the exception of raw global methods, the entry point for most code will be through a constant, whether it be a class constant or a module constant.

We've learned how to create classes and modules. Really, what we are doing is creating constants that point to those objects in memory. When we create constants (classes, modules, or otherwise) in IRB, we are creating a constant in the global namespace. This can quickly get crowded, especially if you are creating a class or module constant that may have a common name.

For instance, in the previous topic, we created an Enum module. Enum is a very common word in the Ruby world, and do we really think our Enum module is the best and that we should own that word? It is possible there is a more official Enum library, or that Ruby may use it as a global constant in the future. Therefore, it's a good practice to name your global constants with a name that is more unique. By doing this, you are also declaring a unique namespace that you can then put other constants inside of to make them safe from name collision.

As such, let's rename our Enum module to be a bit more specific to what the module

is doing:

module ActsAsEnum

end

class PaymentTypes

include ActsAsEnum

end

The name ActsAsEnum is not very inspired, but it is descriptive and as such makes it easy to read and understand what might be happening. While ActsAsEnum is more unique than Enum, in the Ruby world, lots of people use the ActsAs convention, so this may still have issues. We'll go with it for now.

Now that we've defined our ActsAsEnum module, assuming it's unique, we are free to add constants inside the module namespace and we can be sure to avoid name conflicts. In fact, we can use modules for no other purpose than to define namespaces:

module Zippy

SKIPPY = "skippy"

class Zappy

end

module Dappy

def self.say_something

puts "doo"

end

end

end

We defined our arbitrary namespace, Zippy, and created the Skippy, Zappy, and Dappy classes. If we need to access the classes within the namespace, we use the scoping operator, ::, as follows:

Zippy::SKIPPY

Zippy::Zappy.new

Zippy::Dappy.say_something

The output would be something as follows:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.