B
Ben Johnson
Is it possible to determine if a public instance method is being called
from an object or within the class?
Thanks for your help.
from an object or within the class?
Thanks for your help.
Is it possible to determine if a public instance method is being called
from an object or within the class?
Is it possible to determine if a public instance method is being called
from an object or within the class?
I think you might need to be a little more specific with your
question. A class is also an object.
irb(main):001:0> Class.ancestors
-> [Class, Module, Object, Kernel]
I don't think it's possible to grab a method's owner, because methods
can be created in any context on the fly (somebody please correct me
if I'm wrong). So, really what you are probably looking for is the
binding scope.
Just a thought,
Todd
Is it possible to determine if a public instance method is being called
from an object or within the class?
2008/1/23 said:If I understood correctly, you want to look into the call stack. If
so, you must resort to an hack.
I do not see how using #caller is a hack.
I was under the impression that Ben rather wanted a static analysis,
i.e. determine code coverage. This is next to impossible with a
dynamic language such as Ruby.
2008/1/25 said:This is probably in the eye of the beholder. To me, having the call
stack as a single long string
and parsing it to fish out the caller
looks a bit hack-ish. It feels like #caller is supposed to be read by
a human, not by code.
Agreed.
For example, to solve Ben's original problem, I'd rather
have an alternate #caller that returns a chain of calling Objects, or
maybe Methods.
Robert said:I am still waiting for Ben to chime in again.
Cheers
robert
I have not chimed in because I found another solution to my problem and
I realized what I was asking was nearly impossible. I also agree that
caller seems to be used for a human to read and analyze vs using it in
your code.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.