# Accessing the name of the current function

**URL:** <https://www.ruby-forum.com/t/accessing-the-name-of-the-current-function/109541>\
**Category:** Ruby\
**Created:** [July 16, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/accessing-the-name-of-the-current-function/109541 "2007-07-16T17:41:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 16, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/accessing-the-name-of-the-current-function/109541/1 "2007-07-16T17:41:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 16, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/accessing-the-name-of-the-current-function/109541/2 "2007-07-16T18:02:30Z")

</div>

Ronald F. 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

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 16, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/accessing-the-name-of-the-current-function/109541/3 "2007-07-16T18:15:39Z")

</div>

On Jul 16, 10:56 am, “Ronald F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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}”

Facets has this Kernel method for it.

module Kernel

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

```

end

See [http://facets.rubyforge.org](http://facets.rubyforge.org).

I think the plan for 1.9 is to have **method** and **callee** , which  
differ based on aliasing. (Guess Matz decided **shadow** methods are  
here to stay).

T.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [July 16, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/accessing-the-name-of-the-current-function/109541/4 "2007-07-16T18:22:40Z")

</div>

The caller() method has what you’re looking for.

Here’s my equivalent of what you do:

dbg ‘some stuff’, an\_object

def dbg \*args  
if ENV[‘RAILS\_ENV’] == ‘test’  
stack = caller(1).slice(0, 5)  
$stdout \<\< “==========================\n”  
pp(\*args)  
stack.each do |s|  
puts s  
end  
end  
end

- James M.
