RubyInline has the following extension to Object:
def caller_method_name() /\`([^\']+)\'/.match(caller(2).first) end
Is there a better way??? caller(n) returns a string, and I have to
parse it for the method name. This is scary at best. I’d much rather
have something that gave me either the method instance itself, or some
instance representation of the call stack. Something like:
class CallStack def line ... end # returns the line of the calling method at this
def method … end # returns the calling method ATL
def class … end # returns the class for the calling method ATL
def to_s … end # equivalent output to the regular caller(n)
def up … end # returns a CallStack instance for one level up,
def down … end # returns a CallStack instance for one level
down, or nil
and a method, callstack() that returns the current instance of
CallStack. Or something like that. it would change my above method to:
def caller_method_name() callstack.up.up.method.name end
Which reads MUCH better, and you can manipulate a lot more with this
type of expression.
I suppose up and down should also take int args like caller.
Now I’m tempted to go implement this… Just not sure how my ruby-fu
in the internals are.