From: Daniel Lucraft Date: 2007-07-17T01:02:34+09:00 Subject: Re: Accessing the name of the current function Ronald Fischer wrote: > Is there away to access the name of the currently defined function? > Example: Right now I do tracing like this: > > def myfunc(a,b) > trace.debug "ENTER myfunc a=#{a} b=#{b}" > .... > trace.debug "LEAVE myfunc result=#{result}" > result > end > > If I rename myfunc, I also have to change the word myfunc in the > trace statements. It would be convenient to have something like > > trace.debug "ENTER #{__function__} a=#{a} b=#{b}" > > Maybe Ruby already provides such a feature, and I just don't know it... > > Ronald Include this somewhere: class Object def current_method caller[0] =~ /\d:in `([^']+)'/ $1 end end Then use it: def abc puts "ENTERING: #{current_method}" ... end Gives: "ENTERING abc" best, Dan -- Posted via http://www.ruby-forum.com/.