 |
Forums |
Admin Start New Thread
By: Dan Rathbun
RE: Getting Namespace & Nesting Info Internally [ reply ] 2010-03-26 03:44
|
OK Dan... the note in the file didn't explain what you were after. The posting at ruby-talk is more clear. (And thanks for that link, I was wondering where the ruby forums were.)
So you wish that an instance object would be able to know who it's "author" is ?? (I purposely avoid using familial terms, because it they are usually confused with the class-object hierarchy. I also avoid using the term "owner" as it's really Ruby that creates, owns and manages all objects. A context, such as a class or module, can only cause an object instance to be created, and have the first of what may be many references assigned to point at that object. And so I use the term "author.")
I see two scenarios: (Easy and Difficult.)
(1) If the instance objects are created by YOU, inside contexts (Class or Module,) that are YOUR definitions, then the solution is easy.
(2) If it's everyone else's instance objects, created by Classes or Modules that YOU do NOT control, then a solution will be difficult, and perhaps immpossible without a change within the Ruby core.
Which scenario is it that your after?
|
By: Dan Rathbun
Getting Namespace & Nesting Info Internally [ reply ] 2009-11-24 11:25
|
in Windows/api.rb, line 166, Dan et al, poses the question, viz:
"# TODO: If there's a way to automatically grab the namespace internally, nesting and all, I'd love to know the solution."
ANS: class Module, has class method: "nesting"
(from the book):
Module.nesting -> anArray
Returns the list of Modules nested at the point of call.
code example:
module M1
module M2
$a = Module.nesting
end
end
$a » [M1::M2, M1]
$a[0].name » "M1::M2"
Also note the Module class instance method:
self.included_modules -> Array
which is sort of the opposite, it looks down instead of up; and makes an array of included modules.
An there's also:
self.ancestors -> Array
Makes an array of module "self's" superclasses.
Is that the answer ya' was looking for?
|
|
 |