Lets consider I have a class inside a really long module path:
sux = Really::Long::Module::Path::Sucks.new
Could I somehow "import" this module in a way that I could just use the class without worrying writing this path every time I use it?
EDIT: I know being in the same module makes things easier. But I can't be in the same module in this case.
The value of __FILE__ is a relative path that is created and stored (but never updated) when your file is loaded. This means that if you have any calls to Dir.
Creating Modules in Ruby To define a module, use the module keyword, give it a name and then finish with an end . The module name follows the same rules as class names. The name is a constant and should start with a capital letter. If the module is two words it should be camel case (e.g MyModule).
A Ruby module can contain classes and other modules, which means you can use it as a namespace.
As with class methods, you call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.
Modules are an object in ruby, so you can just make a reference to the module that is shorter.
Sux = Really::Long::Module::Path::Sucks
Sux.new
In your class:
include Really::Long::Module::Path
This basically mixes all of that module's constants/methods into the including class, so you can then use the Sucks
class directly:
sux = Sucks.new
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With