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
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace.mobi
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.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12358)
Hello! Python by Anthony Briggs(9715)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9611)
The Mikado Method by Ola Ellnestam Daniel Brolund(9592)
Dependency Injection in .NET by Mark Seemann(9127)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8123)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7633)
Grails in Action by Glen Smith Peter Ledbrook(7542)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7360)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6257)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(5937)
Microservices with Go by Alexander Shuiskov(5669)
Practical Design Patterns for Java Developers by Miroslav Wengner(5587)
Test Automation Engineering Handbook by Manikandan Sambamurthy(5568)
Kotlin in Action by Dmitry Jemerov(4901)
Angular Projects - Third Edition by Aristeidis Bampakos(4901)
The Art of Crafting User Stories by The Art of Crafting User Stories(4460)
NetSuite for Consultants - Second Edition by Peter Ries(4445)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(4243)
