Current method

Is there a way to programmatically determine the name of the method that is
currently being executed?
I’d like to use this for logging purposes.

···

WARNING: All e-mail sent to and from this address will be received or
otherwise recorded by the A.G. Edwards corporate e-mail system and is
subject to archival, monitoring or review by, and/or disclosure to,
someone other than the recipient.


you could do something like this:

def meth
log "Message"
end

def log(message)
puts caller[0]+" "+message
end

meth #=> test.rb:2:in `meth’ Message

The caller array lists the file/line#/method that called the current
method, so log is called by “meth” and can output that

There may be a better way.

regards,

rich

···

-----Original Message-----
From: Volkmann, Mark [mailto:Mark.Volkmann@AGEDWARDS.com]
Sent: Tuesday, October 01, 2002 11:08 AM
To: ruby-talk ML
Subject: current method

Is there a way to programmatically determine the name of the method that
is currently being executed?
I’d like to use this for logging purposes.



WARNING: All e-mail sent to and from this address will be received or
otherwise recorded by the A.G. Edwards corporate e-mail system and is
subject to archival, monitoring or review by, and/or disclosure to,
someone other than the recipient.



Hi Mark,

One “ugly” way is to use the Exception object:

def my_method
    begin
        raise
    rescue => detail
        detail.backtrace[0] =~ /in `(.*)'/
        puts $1
    end
end
my_method    # >> 'my_method'

Hopefully some other people can give a better idea.

Regards,

Bill

···

===========================================================================
Volkmann, Mark Mark.Volkmann@agedwards.com wrote:

Is there a way to programmatically determine the name of the method that is
currently being executed?
I’d like to use this for logging purposes.

Here is what I had to do for RubyInline (1):

def caller_method_name()
  /\`([^\']+)\'/.match(caller(2).first)[1]
end

Note, this is two levels up, you want one level up. Perhaps:

def my_method_name()
  /\`([^\']+)\'/.match(caller(1).first)[1]
end

is an appropriate name.

  1. RubyInline download | SourceForge.net
···

On Tuesday, October 1, 2002, at 08:07 AM, Volkmann, Mark wrote:

Is there a way to programmatically determine the name of the method
that is currently being executed? I’d like to use this for logging
purposes.

Wow, that is a much more clever and much more elegant solution by using
the “caller” method.

May I push it a little bit further? What is the best way to put the log
in all the (or some selective) methods so that we don’t have to write the
“log” calls in all those methods manually? Will aspect oriented
programming (aspectR) play a role in this?

Regards,

Bill

···

==========================================================================
Rich Kilmer rich@infoether.com wrote:

def meth
log “Message”
end

def log(message)
puts caller[0]+" "+message
end

You could probably find some code to facilitate this in tracer.rb.

···

On Wed, Oct 02, 2002 at 03:20:45AM +0900, William Djaja Tjokroaminata wrote:

May I push it a little bit further? What is the best way to put the log
in all the (or some selective) methods so that we don’t have to write the
“log” calls in all those methods manually? Will aspect oriented
programming (aspectR) play a role in this?


Alan Chen
Digikata LLC
http://digikata.com

Check out the chapter on “Reflection, ObjectSpace and Distributed Ruby” in
the Programming Ruby book. It talks about Retrieving a list of all methods
loaded in the program and ways to ‘alias’ or rewrite methods dynamically.

···

At 03:20 AM 10/2/2002 +0900, you wrote:

Wow, that is a much more clever and much more elegant solution by using
the “caller” method.

May I push it a little bit further? What is the best way to put the log
in all the (or some selective) methods so that we don’t have to write the
“log” calls in all those methods manually? Will aspect oriented
programming (aspectR) play a role in this?

Keep in mind that tracer.rb uses set_trace_func, which can slow a
program to a crawl.

Paul

···

On Wed, Oct 02, 2002 at 03:32:29AM +0900, Alan Chen wrote:

On Wed, Oct 02, 2002 at 03:20:45AM +0900, William Djaja Tjokroaminata wrote:

May I push it a little bit further? What is the best way to put the log
in all the (or some selective) methods so that we don’t have to write the
“log” calls in all those methods manually? Will aspect oriented
programming (aspectR) play a role in this?

You could probably find some code to facilitate this in tracer.rb.