S
Steven Parkes
I'm trying to do nested class/module lookup by name and beginning to =
think it's not possible/clean in the face of autoloading.
The lookup is typical/simple String =3D> Module:
cls =3D cls.split("::").inject(Object) {|scope, const_name| =
scope.const_get(const_name)}
Trouble arrises when you use autoloading (the nested names aren't =
already defined) and someone creates a class of the same name at the top =
level/Object scope. In that case, the const_get will resolve to the top =
level rather than loading the nested object. (Actually, it'll occur if =
anyone creates a class of the same name in any parent ...)
Alternatives seem to be:
- don't use autoloading
- eval a string to get the class (not sure if this even works)
- unique-ify all your class names everywhere, not just per-scope
Am I missing anything?
think it's not possible/clean in the face of autoloading.
The lookup is typical/simple String =3D> Module:
cls =3D cls.split("::").inject(Object) {|scope, const_name| =
scope.const_get(const_name)}
Trouble arrises when you use autoloading (the nested names aren't =
already defined) and someone creates a class of the same name at the top =
level/Object scope. In that case, the const_get will resolve to the top =
level rather than loading the nested object. (Actually, it'll occur if =
anyone creates a class of the same name in any parent ...)
Alternatives seem to be:
- don't use autoloading
- eval a string to get the class (not sure if this even works)
- unique-ify all your class names everywhere, not just per-scope
Am I missing anything?