Reputation: 998
I want to add a namespace to a class/module I'm defining, but I don't want to type in the long format
class A::B::C::D::<ClassName>
OR
module A
module B
module C
module D
class ClassName
end
end
end
end
end
In other words, I just want to define the class class ClassName
and then need a way to send this class to live under the namespace A::B::C::D
So that I can write a bunch of code like so:
# File Name: a/b/c/d/some_class.rb
class SomeClass
include SomeModule
# class content
end
# File Name: a/b/c/d/some_module.rb
module SomeModule
# module content
end
Because there are too many modules in my codebase, I don't want to type in the prefixing namespace everytime. But I want to say something like this at the end:
SomeClass.add_to_namespace(A::B::C::D)
SomeModule.add_to_namespace(A::B::C::D)
Important: This should also affect class namespace lookup so that Ruby/Rails doesn't complain like:
expected a/b/c/d/some_class.rb to define SomeClass
Question: Will include
-ing my class in the namespace have the same effect?
module A::B::C::D
include ClassName
end
Upvotes: 0
Views: 1239
Reputation: 388
It seems like what you're asking is kinda multi-faceted, but if you just want to use the class in another file without having to type out the namespace each time then a simple solution would just be to redefine it in the file you want to use it in
ClassName = A::B::C::D::ClassName
Then you can just do ClassName.method
just fine
Upvotes: 2