How not to write the full module path in ruby?

Let's look at the fact that I have a class inside a very long module path:

sux = Really::Long::Module::Path::Sucks.new

Is there any way to “import” this module so that I can simply use the class without worrying about writing this path every time I use it?

EDIT: I know that being in one module makes things easier. But I cannot be in the same module in this case.

+5
source share
3 answers

Modules are an object in ruby, so you can just make a link to a shorter module.

Sux = Really::Long::Module::Path::Sucks
Sux.new
+4
source

In your class:

include Really::Long::Module::Path

/ , Sucks:

sux = Sucks.new
+3
module A; module B; module C; module D
  class E; end
end; end; end; end

class Sanity
  include A::B::C::D
  puts E.new.object_id
end
+2

All Articles