How not to write full module path in ruby?
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 wo开发者_运维知识库rrying 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.
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
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
精彩评论