# Getting name of current method

**URL:** <https://www.ruby-forum.com/t/getting-name-of-current-method/194510>\
**Category:** Ruby\
**Created:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/getting-name-of-current-method/194510 "2010-09-06T16:39:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/getting-name-of-current-method/194510/1 "2010-09-06T16:39:53Z")

</div>

Ruby has **FILE** and **LINE** to give the file and line omber where we  
are currently at in the code.

Consider  
Class X  
def my\_method  
m = ??? # I want m to be assigned “my\_method”  
end  
end

How can I get m set properly?

I guess I could use define\_method to define my\_method and then pass the  
name into the function … but that is so ugly and also flattens scope  
that I may not want flattened.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/getting-name-of-current-method/194510/2 "2010-09-06T16:39:55Z")

</div>

On Sep 3, 2010, at 17:44 , Ralph S. wrote:

> Ruby has **FILE** and **LINE** to give the file and line omber where we are currently at in the code.
> 
> Consider  
> Class X  
> def my\_method  
> m = ??? # I want m to be assigned “my\_method”  
> end  
> end
> 
> How can I get m set properly?
> 
> I guess I could use define\_method to define my\_method and then pass the name into the function … but that is so ugly and also flattens scope that I may not want flattened.

`ri Kernel.caller`

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 6, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/getting-name-of-current-method/194510/3 "2010-09-06T16:44:06Z")

</div>

On Sat, Sep 4, 2010 at 1:44 AM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ruby has **FILE** and **LINE** to give the file and line omber where we are currently at in the code.
> 
> Consider  
> Â Class X  
> Â Â def my\_method  
> Â Â Â m = ??? Â # I want m to be assigned “my\_method”  
> Â Â end  
> Â end
> 
> How can I get m set properly?

> > class X  
> > def my\_method  
> > m = **method**  
> > end  
> > end  
> > =\> nil  
> > X.new.my\_method  
> > =\> :my\_method

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 6, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/getting-name-of-current-method/194510/4 "2010-09-06T16:44:09Z")

</div>

Ryan,

Friday, September 3, 2010, 6:50:53 PM, you wrote:

RD\> On Sep 3, 2010, at 17:44 , Ralph S. wrote:

> > Ruby has **FILE** and **LINE** to give the file and line omber where we are currently at in the code.

> > Consider  
> > Class X  
> > def my\_method  
> > m = ??? # I want m to be assigned “my\_method”  
> > end  
> > end

> > How can I get m set properly?

> > I guess I could use define\_method to define my\_method and then pass the name into the function … but that is so ugly and also flattens scope that I may not want flattened.

RD\> `ri Kernel.caller`

Thanks for pointing that out. Interesting and useful.

I have come up with two alternate methods and I’d like to ask the help  
of all those here to help me modify the first method so that it does  
what I want

* * *

class X  
@m = “method\_1”  
@s = \<\<-XXX  
def #{@m}  
puts “In #{@m}”  
end  
XXX

puts “#{ **FILE** } @ #{ **LINE** }”  
puts @s  
eval @s

m = “method\_2”  
eval(“def #{m}; puts “In #{m}”; end”)

end

x = X.new

x.method\_1  
x.method\_2

* * *

The code, above, works fine in 1.8.6

Note that I have to define @m before @s. How can I set things up so  
that I can define @m after @s so that I can modify @m and get a new  
function?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 6, 2010, 4:52pm UTC](https://www.ruby-forum.com/t/getting-name-of-current-method/194510/5 "2010-09-06T16:52:23Z")

</div>

On 04.09.2010 06:09, Ralph S. wrote:

> Note that I have to define @m before @s. How can I set things up so  
> that I can define @m after @s so that I can modify @m and get a new  
> function?

What are you trying to achieve?

Kind regards

robert
